How comprehensive is the WebSocket implementation?

Here goes,

I’m newer to Defold and Lua but completed the “War battles tutorial” and feel pretty good about how things worked.
While trying to get an echo from the example at
API reference (extension-websocket) (
I ran into some issues. It looks like the server may no longer be available and I’m getting this error when using log

ERROR:SCRIPT: /main/websocket_code.script:10: attempt to call global 'log' (a nil value)
stack traceback:
 /main/websocket_code.script:10: in function </main/websocket_code.script:1>

Does log require a separate dependency?

Now onto the main question.

Does the WebSocket extension support using secure websockets?
Can you run a WebSocket server from Defold? Or rather is there a higher level API that allows creation of a websocket server without implementing the protocol yourself?

After doing some research I think the answer to both of the above may be no. After reading this post and an earlier one before signing up to the forum.

My intended use case is to run an instance of Defold as a WebSocket server per game match.

Where is this coming from? Is the websocket_code.script your own? There is no built in log function, but you may have picked that up from some example? To print/log something you usually only do



We only provide a WebSocket client, but there’s nothing stopping you from implementing a server as well.

The websocket_code.script contains code I pasted from the API reference.

local function websocket_callback(self, conn, data)
	if data.event == websocket.EVENT_DISCONNECTED then
		log("Disconnected: " .. tostring(conn))
		self.connection = nil
	elseif data.event == websocket.EVENT_CONNECTED then
		log("Connected: " .. tostring(conn))
	elseif data.event == websocket.EVENT_ERROR then
		log("Error: '" .. data.message .. "'")
	elseif data.event == websocket.EVENT_MESSAGE then
		log("Receiving: '" .. tostring(data.message) .. "'")

function init(self)
	print("This is firing")
	self.url = "ws://"
	local params = {
		timeout = 3000,
		headers = "Sec-WebSocket-Protocol: chat\r\nOrigin:\r\n"
	self.connection = websocket.connect(self.url, params, websocket_callback)

function finalize(self)
	if self.connection ~= nil then

I tried adding this library.
subsoap/log: General purpose logging for Defold (

But, I am unsure where to add the

local log = require("log.log")

I will spend more time learning Lua and Defold on my own though.

Maybe somebody could answer or point me in the right direction on the starting point for implementing a websocket server?
Would a websocket server be something I could implement on my own in the Defold editor with Lua or would that require getting into the lower level nuts and bolts of the engine or extension?

I have looked over this page from Mozzilla.
Writing WebSocket servers - Web APIs | MDN (

I guess I’m thinking that to create a websocket_server I would need to implement it on top of an existing TCP server? Are there existing facilities in Defold to run a TCP server? Or would I be starting from UDP?

Or is binding a TCP socket to a port enough to begin listening in Defold?

My knowledge gap may be too great in this area.

Oh, I see. Sorry about that. Just replace log() with print(). I’ve updated the docs:

It should be up on the website in a minute or two.

I don’t know exactly how a WebSocket server works, but if it’s anything like a TCP server or standard webserver for http requests then it will indeed require quite a bit of work. You need to manage multiple connected clients and probably also some logic around the handshake/connect process. It will require work similar to what you find in the WebSocket extension:

Yes, that’s enough, but it won’t really help you with WebSocket clients. (Here’s an example of a very basic TCP server:

Thanks @britzl , this is very helpful and gives me some ideas and plenty to think about. :slight_smile:

I just want to note that the C library that we use (Wslay) supports both client/server.
It’s just that we haven’t implemented the server part in our extension, but that shouldn’t be too hard for someone used to C/C++. As a reference, one can look at the implementation of websockets in Godot (using the same library).

Ah, I didn’t know that! That will make it a little bit easier at least!

Thanks for the info, it seems like this is where I should start. And I imagine would end up being worth it in the end as opposed to trying to build some ad hoc state and collision manager on some existing service.

@britzl this is getting off topic, but I was looking at the gamelift extension
defold/extension-gamelift: Amazon Gamelift extension for the Defold game engine (
and saw this
“GameLift Amazon GameLift extension for the Defold game engine. Run Defold on the server with GameLift SDK support!”
Do you know if it supports HTML5 clients?

This game used a headless Linux build as the server on GameLift.

It looks like quite the technical feat and I know Android can really be a nightmare… at least that’s what I’ve heard, but so can web :sweat_smile:.

What they did was have the same game and bundle it for two different scenarios: server and client.

It’s easy to do using different “–settings” arguments if you are using bob.jar to bundle the game.

I will need to look into the bob the builder command tool. I was more talking about how it was probably developed with “plain” sockets. It would be nice if browsers could use UDP, or well nicer for me.

GameLift only helps you spin up a server. The capabilities of that server is for you to decide :slight_smile: But yes, if you want your server to support HTML5 clients then that’s totally doable as long as you chose a network protocol that a web client can handle (such as WebSockets).

Ok, that makes sense. I think my plan is to start with the wslay library by itself to try and wrap my head around it and then explore the Defold native extension example and go from there. Thanks for the info.

1 Like