Defold-WebSocket (DEPRECATED)

I’ve decided to move my asynchronous WebSocket module from DefNet to a separate repo. The module has also received a number of improvements and the underlying lua-websocket project has been updated. The new version should in theory also work with secure WebSockets thanks to the use of LuaSec. Defold-WebSocket is currently used in the Defold client of the Colyseus game server and client lib.

Check the asset page for details and an example of how to use the module:


I noticed the secure websocket does not work for HTML5 version, it indeed works for other kinds of bundles.

For HTML5 version, there is an error message like “MyProject.js:1 WebSocket connection to ‘ws://domain:port/’ failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE”

In fact, I specified the target url '‘wss://domain:port/’ instead of ‘ws://domain:port/’. I don’t know if this problem is easy to fix since I already get openssl / luasec extension built with emscripten.

I also think about if I can implement the web scoket with javascript and call it from lua (for HTML5 version)?

Hmm, I thought secure websockets worked in html5. @endel?

No, it does not. luasec does NOT have proper openssl lib for html5, and its core functionality is comments out for html5.

Hold on, for HTML5 there’s no use of LuaSec. Emscripten will automatically upgrade socket connections to Websockets. That’s why the Defold-Websocket project has two code paths in many places to for instance skip the handshake and socket upgrade etc. The readme should documentat thus fairly well.

1 Like

Trying to get this to work with an empty project, but I am getting a build error. Thoughts?
Editor: 1.2.139 on Windows 10


Replace your luasec with luasec by @britzl
It fixed for working with clang compiler


That and after an editor restart fixed it. Thanks!


How did you know to put in that specific LuaSec version by sonountaleban and not the fork by me with the fix?

I actually grabbed the incorrect link from your page:


Didn’t realized it was pointing to a different link until @AGulev pointed it out.

Ah, I see. Well, it’s a fork and I didn’t think to edit the readme. Fixed now!


Great stuff btw.

I’m just testing the basics right now, so far I’m getting connected properly and able to send messages back and forth.

However, I don’t seem to be getting an error event when the client can’t connect to the server? Like if the server is offline. How can I capture this?

Also, if I terminate the server abruptly, I’m also not getting a disconnected event?

You should be able to catch errors in the client. I believe Colyseus does this.

Does it support html5 wasm projects exported by Defold?

Yes, sure, I just tried the example project and it connects as expected. Any reason why it shouldn’t work with web assembly?

Great, great!
I just new to Defold so…
Anyway thanks for reply.

With an empty clean project, and this code based on the example page:

local client_async = require "websocket.client_async"

function init(self) = client_async(), err)
		if ok then
			print("Connected")"#", "acquire_input_focus")
			print("Unable to connect", err)

function update(self, dt)

It prints the message properly if the server is running. But if the server is not running, the “Unable to connect” does not get printed.

I also tried Colyseus, and it’s the same thing. The “on error” event there also doesn’t get triggered.

I tried it on the latest editor 1.2.142, but I’ve never been successful in older versions either. I also tried html5 build, I do get a websocket error there.

However, this error is not captured in code (i.e. can’t connect error doesn’t even get printed in the console).

How do I capture this connection refused properly in code, so I can display an error message properly? Potential workaround is to have a timeout e.g. if after 2 seconds I am still not connected, then I can assume something is wrong with the connection. But I am hoping this is something the extension is capturing properly, and I just don’t know how to make it work.


If you are using Colyseus, you should not to connect immediately:

Strange but I just test your code with example project. It prints the error message on desktop(mac) if the server is not running.

Could you please pprint or debug the err and ok?

Also it is better to use err

if err then
  print("Unable to connect", err)

1 Like

I’ve added the pprint you suggested.

Here’s what I get if the server is NOT running :

For comparison, here’s what I get if the server is running.

I am using Windows 10. I don’t have access to a Mac right now, but I will try over there when I get the chance. Actually, I am also not getting the Disconnected event (if I terminate the server while the Defold client is connected).

For Colyseus, yes my set-up is the same (call to connect is after the listeners have been registered), but sadly the same results.