DefNet - Defold networking modules


I often get questions about networking in Defold. Sometimes it is as easy as “How do I make an HTTP call?” and I can point to the documentation for http.request() and everyone is happy. Other times the questions are more complex (often relating to socket connections) and in those cases the Defold documentation isn’t enough.

To remedy this lack of information and good examples I decided to collect some Lua modules in a new repository and share them with you. The repo currently contains modules to perform P2P discovery, to create TCP socket servers and clients and finally an example of how to build an HTTP server on top of a TCP socket. You can find the examples here:



Awesome. I am starting up on a barebone UDP framework as well. (with a simple ack layer for reliability on top). Will look into this and maybe could add something to the collection later on.



Cool! I’m looking into some kind of UDP setup myself for a virtual gamepad app I’ve been tinkering with. Let’s talk when we meet at the office next time.



Thanks much @britzl , especially for the examples.

I would like to register my interest also, for any further related information; I’m especially interested in the inter-netting of devices via bluetooth(I assume this will have to wait until the ‘plugin-system’ is developed).

My intended application of Defold engine is in real-time, inter-device educational software, for the classroom.




Exactly Anna! Once we have the extension system in place it should be possible to create a bluetooth extension of some kind. I assume you want to use bluetooth instead of wifi because the classroom doesn’t have any wifi?



One of the FutureGames students found this bitstream library to use in his game project:

It also has support for high level messages and message deltas. I haven’t tried it myself but it seems like it could fit in nicely with a TCP or UDP socket solution.



Yes, and also because during prior tests I have found the wifi to be unreliable/high-latency. We have resolved this by providing battery powered wireless access points which the teacher can place anywhere , including when outside the class; I would like to eliminate this dependency.



Hey @britzl, if it possible can you extend this module with more specific examples. Say, how to setup a server + client_01 + client_02 structure for a simple platformer (or any other) game and replicate character position from the client_01 to the server and then to the client_02.

I mean with the current example I can understand (more or less) how to launch server and client, but I have no idea how to deal with, for example, a character position or health.

1 Like


@Hio: I’ve created a local network multiplayer example here:

The example will listen for an instance of the application acting as a host or announce its presence as a host for other clients to connect to if no host is currently broadcasting on the local network. When the host receives a connection its is forwarded to all the other clients. The clients themselves communicate directly with each other and repeatedly send a heartbeat message to the host. If a client is shut down or for some other reason is unable to send heartbeat messages it will be removed and the other clients will be notified.

There’s plenty of room for improvement, and I’m really no expert in network coding, but hopefully it will at least give you an idea!


Html Online games?

Wow! :open_mouth: I didn’t expect you to give me working example within only two days. Thanks!



@britzl add DefNet to Assests pages.



Hey Britzl, I’m working on a new mobile game. I want to have a play mode where 2 people can play against each other on seperate phones. Can I set that up with the code/info that you’ve linked to here? Pretty much I just want to know if the aforementioned set up is possible, before I get too carried away. Cheers



Hi. It’s hard to say exactly what you need. What kind of game is it? Real-time? Turn-based? Do you need any kind of server logic or is all state on the clients? How should the two players find each other? Via a server-side lobby? Via P2P discovery on the same wifi? Using Bluetooth? Many question, I know, but I need to know more before giving an answer.



Thanks for the quick reply. As of now it’s going to be single-screen RTS, with hopefully a versus option. I’d like people to be able to challenge someone in their immediate vicinity, but if no one’s available then also be able to find some one to battle online if they wanted. I can see that this is going to be a lot of work and my technical knowledge of networking is limited at the moment, so if I had to choose one thing to include it would be the first option i.e. battle someone close by, so maybe a bluetooth option would be the best way to go?



Bluetooth or P2P discovery+sockets could work for your scenario. I’ve been meaning to look into a Bluetooth Native Extension, but for now none exists. An example of P2P discovery can be seen in DefNet and it could be worthwhile for you to look into.

Another option would be to look into the matchmaking functionality of a service such as PlayFab or perhaps Google Play Games and once the matchmaking is done on the server you’d hand over to the clients to battle using direct socket connections without involving any backend. The thing with not having a server is that there’s no way for you to prevent cheating and hacked clients.



Thanks for those options. I’ll investigate and see what I can come up with. This game dev thing is actually getting really fun now. It’ll be good to add some more skills to the repertoire :sunglasses:



Hey @britzl,
thank you for these awesome modules!
I tryed your multiplayer implementation with a Android - Windows and a Android - Andoid setup.
Both times the Server (Game i started first) doesnt receive position messages after the second player joined. But the second player gets position updates from player 1.

I tryed to find the error my self but didnt success…



The multiplayer thing should be seen as a sort of proof of concept thing, not a finished module to drop in and use in production code. For real-time I’d probably look at either Gamesparks RealTime or Photon Engine, or perhaps your own custom server using LuaSocket.



But if i want peer to peer i just need that things for bringing the players together. Or am i wrong?

My plan was to give the players the IP of the opponent and then let them play with help of this module. I do roundbased and not realtime. Like a pvp chess game.

Meh i was rly happy that that finally worked :frowning:



The peer to peer discovery should work reasonably well, at least the last time I tried. Don’t the clients find each other?