Websocket how to send binary text messages , also does it support wss?

  1. In what format does the Defold client send messages? Is it pure text? How can I send and receive text in binary format, similar to how it can be done in JavaScript using, for example, connection.binaryType = "arraybuffer"? Also, how can I receive and parse binary text from the server?

  2. Does the extension support secure WebSocket sending with the wss:// prefix?

  3. The final question: How does Defold load the WebSocket extension without compiling it, only using the zip file? Where can I read about how it accomplishes this?

my defold code :

local M = {}

M.WSURL = "ws://localhost:8001/test"

function M.websocket_callback(self, conn, data)
	if data.event == websocket.EVENT_DISCONNECTED then
		print("Disconnected: " .. tostring(conn))
		self.connection = nil
	elseif data.event == websocket.EVENT_CONNECTED then
		print("Connected: " .. tostring(conn))
	elseif data.event == websocket.EVENT_ERROR then
		print("Error: '" .. tostring(data.message) .. "'")
		if data.handshake_response then
			print("Handshake response status: '" .. tostring(data.handshake_response.status) .. "'")
			for key, value in pairs(data.handshake_response.headers) do
				print("Handshake response header: '" .. key .. ": " .. value .. "'")
			print("Handshake response body: '" .. tostring(data.handshake_response.response) .. "'")
	elseif data.event == websocket.EVENT_MESSAGE then
		print("Receiving: '" .. tostring(data.message) .. "'")

return M

then calling it :

local networkmaneger = require ("main/scripts.network_maneger")

function init(self)

	self.active = false

	self.url = networkmaneger.WSURL
	local params = {
		timeout = 300000,
		headers = "Sec-WebSocket-Protocol: chat\r\nOrigin: mydomain.com\r\n"
	self.connection = websocket.connect(self.url, params, networkmaneger.websocket_callback)

function on_message(self, message_id, message, sender)
	if message_id == hash("show_level_select") then 
		self.active = true
	elseif message_id == hash("hide_level_select") then 
		self.active = false

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed and self.active then 
		print("button pressed1111")
		local message_to_send = 'sending to server'
		local ok, was_clean, code, reason = websocket.send(self.connection, message_to_send)
		print("Sending '" .. message_to_send .. "'", ok, was_clean, code, reason)

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


1 Like
  1. it uses the Lua string you pass to it. A Lua string can contain binary data.

  2. yes, it supports wss

  3. ” How does Defold load the WebSocket extension without compiling it”

It does compile it into the executable,just like any ithe native extension.

Thanks ,
is there any flags i need to add to make it support secure ? and binary ?
regarding 3 , i didn’t saw any compilation going on when i added the zip to the dependencies …

You need to connect to wss://mydomain.com and not ws://mydomain.com

Everything you send is stream of bytes, represented by a Lua string. You don’t need to do anything in particular. Generate bytes in a string and send it.

The first time you build a project with native code, the native code is sent to build.defold.com, and compiled and linked into a custom engine which gets downloaded and stored in the .internal/cache folder in your project. In the editor you see a “Building engine” progress text while building the first time.

1 Like