WebRTC extension

Hi everyone!

I created a small and easy-to-use C++ library to implement WebRTC in Defold.

For now it only works on the Web platform, but since it uses paullouisageneau’s libdatachannel it is highly cross-platform and should work on desktop and mobile with only some minor adjustments, I’m currently having problems compiling the libraries (especially OpenSSL for native platforms).

I tried to make it simple to use, taking inspiration from the websocket extension, here’s a simple test:

-- Called as soon as a signaling data it's created. (eg. offer, answer, candidates)
function on_signaling_data(self, data)

function on_channel_info(self, event. id, label)
    if event == "opened" then
        print("Data channel \'" .. label .. "\' with " .. id .. " created!")
        webrtc.send_message(id, label, "Hello!")
    elseif event == "closed" then
        print("Data channel \'" .. label .. "\' with " .. id .. " closed!")

function on_channel_message(self, id, label, msg)
    print("[" .. id .. "] Got message from \'" .. label .. "\':" .. msg)

-- We're assuming the server only sends signaling data.
function on_server_message(self, message)

function init()
    webrtc.set_configuration("stun_url:PORT;turn_url:PORT:USER:PASSWORD", on_signaling_data, on_channel_info, on_channel_message)
    -- If we're the offerer, create a channel with another peer.
    webrtc.create_channel(peer_id, "my_channel_name", webrtc.TYPE_RELIABLE)

For now, it has only some basic features:

  • Creating channels with other peers using integer ids
  • Adding a list of STUN and TURN servers to use
  • Choosing the type of the channel, it can be RELIABLE, UNRELIABLE, and UNRELIABLE_ORDERED
  • Sending simple text messages

There isn’t a websocket backend, the developer is free to decide to use whatever method they want for the signaling part (just in case there will be an example that uses the Defold websocket extension).
The project is still in a heavy development phase so it isn’t very stable but it does work: It’s already able to create a connection between two peers behind NATs using a TURN server.

It’s also my first-ever extension for Defold so let me know any feedback you may have and I’ll be more than happy to add it to the project!


Nice! Thank you for sharing! I’m sure this will be useful to people.


Please also submit it to the asset portal: GitHub - defold/asset-portal: List of Defold assets, libraries and extensions

Hi, just pushed a commit:

  • I tried to simplify the callback functions and merge them into one.
    Having to pass multiple functions to set_configuration seemed a bit stiff. Let me know what you think :smiley:
function webrtc_callback(self, event, data)
    if event == webrtc.EVENT_CHANNEL_OPENED then
        print("Data channel \'" .. data.label .. "\' with " .. data.id .. " created!")
        webrtc.send_message(id, label, "Hello!")
    elseif event == webrtc.EVENT_CHANNEL_CLOSED then
        print("Data channel \'" .. data.label .. "\' with " .. data.id .. " closed!")
    elseif event == webrtc.EVENT_SIGNALING_DATA then
    elseif event == webrtc.EVENT_MESSAGE then
        print("[" .. data.id .. "] Got message from \'" .. data.label .. "\':" .. data.msg)
  • I also added a python example for a web socket server that works with the defold one.

  • Lastly I began implementing the native version of libdatachannel. It’s currently for Windows only, but I’d like to make it work on Android and Linux at least.

As of now, I managed to integrate the OpenSSL library thanks to GameAnalytics’s binaries, but I didn’t manage to statically compile the datachannel library for the time being.