Networking, parse, pubnub, etc

Absolutely, Check out:

As sicher has already mentioned, there’s built in support for http and json. The Defold engine also bundles the luasocket library, primarily used for hooking up the engine to an external debugger (ZeroBraneStudio or similar). You can use the socket library for pure socket communication in your game as well.

As for Parse (I assume you mean parse.com) then no, there’s no ready made Lua module for Parse, but the Parse REST API is well documented and easy to implement: https://parse.com/docs/rest/guide

Well, that was all I really needed. I’m gonna test it out, and try to work out some small parse.com module for easy handling parse. Sorry for the noob question, but I’m having some trouble navigating documentation part of defold engine :slight_smile:

1 Like

This is something I’ve been thinking about. Is there a JSON encode method that can do table to JSON? The documentation only mentions decoding.

Since Lua-tables are complex and can contain almost anything, serializing the to JSON in a general way is not trivial, so you would need to create you own implementation that creates JSONs the way you expect.

As Johan said, there’s a certain ambiguity when it comes to Lua tables, and you need to decide how you wish to encode your table into json. There are many existing pure-Lua JSON encode modules for you to chose from. One list of modules can be found here: http://lua-users.org/wiki/JsonModules and you can find a ton more if you Google.

I’m one of the engineers at Heroic Labs and I’m also interested in how best to write a HTTP SDK. I’d like to provide one for our API which we can offer as an open-source library on GitHub for the Defold engine. I have a few of questions about how best to integrate with the http functionality exposed so far.

  • Does the HTTP module handle network retry queues or any kind of “disconnected mode”?
  • Should I handle request pools and exponential retry logic separately?
  • Is there a “Deferred” or “Promise” like structure for asynchronous code execution I can use to wrap network operations? Is there a “blessed” Defold way to handle this?
1 Like

The HTTP module is very bare bones. You can do single HTTP requests (GET, POST etc) with the ability to set headers, POST data and timeout and you get a callback with the response. Nothing more.

You can do multiple HTTP requests at the same time.
You have to write your own retry functionality.
You have to implement your own exponential back-off algorithm.

When you say “disconnected mode” do you then mean a way to detect network state or do you mean some kind of HTTP request caching for when the user is disconnected?

There are several Promise implementations in Lua (one example: https://github.com/zserge/lua-promises). We have our own implementation here at King as well. You can also use coroutines to get a synchronous code flow out of multiple asynchronous calls. Quick and dirty example:

local function get(url, headers)
	local co = coroutine.running()
	assert(co, "You must wrap this in a coroutine")
	http.request(url, "GET", function(self, id, response)
		coroutine.resume(co, response)
	end, headers or {})
	return coroutine.yield()
end


local function do_many_http_calls_one_after_the_other()
	local response1 = get("http://www.acme.com/my/cool/api/endpoint1")
	-- do stuff with the response
	local response2 = get("http://www.acme.com/my/cool/api/endpoint2")
	-- do more stuff
end

coroutine.wrap(do_many_http_calls_one_after_the_other)()
1 Like

Thanks for the detailed reply @britzl

When you say “disconnected mode” do you then mean a way to detect network state or do you mean some kind of HTTP request caching for when the user is disconnected?

When I mentioned “disconnected mode” I meant both for use with request caches as well as notification from the network stack about offline status. Can I register a callback and be notified by the device about network status on both iOS and Android?

I’ll have a go on an initial implementation of our API for Defold. It’ll be lots of fun. Btw, do you plan to open-source the promise implementation from King? :wink:

1 Like

When it comes to network status this can be checked from sys.get_connectivity(). There’s no callback/listener functionality.

There is currently no plan to open source the promise implementation. I have a long term goal of open sourcing some of the Lua modules we’ve created, but I can’t say anything else about it at this time.

1 Like

Hi. I am trying to do a realtime multiplayer game in Defold (lightweight and snappy as it is) and I have some questions related to the networking part of Defold:

  1. Should/Can I use lua modules for websockets or are they implemented already as I would like to have the game in HTML5 also (think Agar.io)?

  2. Is HTTPS supported or should I use a module if it’s possible? Checking the patch notes, I saw something related to fixing a SSL bug.

Thanks :smile:

I’m interested in making a realtime multiplayer game in Defold too. @britzl has mentioned luasocket library that bundled in Defold. Is it https://github.com/diegonehab/luasocket?

Yes, it’s luasocket by Dieogo Nehab that we’re using. We primarily added this to be able to use mobdebug and ZeroBraneStudio for debugging, although having a socket library is useful in many other scenarios as well (multiplayer games, wire protocol for test automation etc). You can find the Lua part of luasocket in builtins/scripts/socket.lua.

1 Like

I’ve been testing real time multiplayer.

Unfortunately websockets seem to not be included in the library. This allows me to bundle multiplayer games that work on all platforms except HTML5.

I’ve tried to use this library https://github.com/lipp/lua-websockets as it was just lua files, but it seems to require luabitop (if not using Lua 5.2 nor luajit). I know that Defold in using Lua 5.1.

Is there any possibility of adding websocket support in the future?

Cheers :smile:

So I tried to do a socket.connect(“echo.websocket.org”, 80) in an HTML build and I see in the Chrome Dev console that “WebSocket is closed before the connection is established” so something is happening in the browser at least. @Fuscy, what have you tried and what have not worked?

I deployed a node.js server using https://github.com/websockets/ws. Used just a simple listener for incoming connections.

On the client side, Defold trying to connect using tcp socket to the server which is successful as the connection doesn’t assert locally.

The problem is that due to the fact that the websockets use tcp, the listener triggers but then a ECONNRESET (Connection reset error in Node.js) happens due to the differences in protocol.

Edit: changed my server implementation a little, it no longer gives any error but it’s still shutting down the connection immediately with no sends/receives getting through.

Hmm, that’s strange. From what I saw in the chrome dev tools and from reading about websockets was that some kind of handshake is done where the client (in this case Defold): https://www.wikiwand.com/en/HTTP/1.1_Upgrade_header#/Use_with_WebSockets

I could see the client initiating the handshake by requesting a websocket via these headers:

  • Upgrade: websocket
  • Connection: Upgrade

Could you please try and verify that your node.js server is accepting websocket from other clients than Defold? I’ll also send an e-mail to the team and ask if anyone is aware of any issues with the socket implementation when running Defold in HTML.

Thanks for looking into this.

I’ll do some further testing also.

Did you ever find a solution? I’m attempting to write a multiplayer game and I’m running into the same issue with websockets.

See this answer: Websocket problem