Skip to content
A cross platform asynchronous implementation of the WebSockets protocol for Defold
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Fix for HTML5 mixed content (#10) Aug 17, 2018
input Initial commit Feb 19, 2018
tools Initial commit Feb 19, 2018
websocket Default to an empty config table if none is provided Dec 6, 2018
.gitignore Initial commit Feb 19, 2018 Initial commit Feb 19, 2018 colorize the syntax as lua on readme (#15) Mar 3, 2019
game.project Updated luasec dependency Mar 18, 2019
logo.png Added logo Feb 21, 2018


This project aims to provide a cross platform asynchronous implementation of the WebSockets protocol for Defold projects. Defold-WebSocket is based on the lua-websocket project with additional code to handle WebSocket connections for HTML5 builds. The additional code is required since Emscripten (which is used for Defold HTML5 builds) will automatically upgrade normal TCP sockets connections to WebSocket connections. Emscripten will also take care of encoding and decoding the WebSocket frames. The WebSocket implementation in this project will bypass the handshake and frame encode/decode of lua-websocket when running in HTML5 builds.


You can use the modules from this project in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

Or point to the ZIP file of a specific release.


This project depends on the LuaSocket and LuaSec projects:

You need to add these as dependencies in your game.project file, along with the dependency to this project itself.


local client_async = require "websocket.client_async"

function init(self) = client_async({
		connect_timeout = 5, -- optional timeout (in seconds) when connecting
	}), err)
		if ok then
			print("Connected")"#", "acquire_input_focus")
			print("Unable to connect", err)
		print("Received message", message)

function update(self, dt)

function on_input(self, action_id, action)
	if action_id == hash("fire") and action.released then"Some data")

Important note on Sec-WebSocket-Protocol and Chrome

Emscripten will create WebSockets with the Sec-WebSocket-Protocol header set to "binary" during the handshake. Google Chrome expects the response header to include the same Sec-WebSocket-Protocol header. Some WebSocket examples and the commonly used Echo Test service does not respect this and omits the response header. This will cause WebSocket connections to fail during the handshake phase in Chrome. Firefox does impose the same restriction. I'm not sure about other browsers.

Testing using a Python based echo server

There's a Python based WebSocket echo server in the tools folder. The echo server is built using the simple-websocket-server library. Start it by running python from a terminal. Connect to it from localhost:9999. The library has been modified to return the Sec-WebSocket-Protocol response header, as described above.

You can’t perform that action at this time.