/datachannel-wasm

C++ Wasm and Native WebRTC Data Channels

Primary LanguageC++GNU Lesser General Public License v2.1LGPL-2.1

datachannel-wasm - C++ WebRTC Data Channels for WebAssembly in browsers

Join the chat at https://gitter.im/libdatachannel/datachannel-wasm Discord

datachannel-wasm is a C++ WebRTC Data Channels and WebSocket wrapper for Emscripten compatible with libdatachannel.

datachannel-wasm exposes the same API as libdatachannel, and therefore allows to compile the same C++ code using Data Channels and WebSockets to WebAssembly for browsers in addition to native targets supported by libdatachannel. The interface is only a subset of the one of libdatachannel, in particular, tracks and media transport are not supported. See what is available in wasm/include.

These wrappers were originally written for my multiplayer game Convergence and were extracted from there to be easily reusable.

datachannel-wasm is licensed under LGPLv2, see LICENSE.

Installation

You just need to add datachannel-wasm as a submodule in your Emscripten project:

$ git submodule add https://github.com/paullouisageneau/datachannel-wasm.git deps/datachannel-wasm
$ git submodule update --init --recursive

CMakeLists.txt:

[...]
add_subdirectory(deps/datachannel-wasm EXCLUDE_FROM_ALL)
target_link_libraries(YOUR_PROJECT datachannel-wasm)

Since datachannel-wasm is compatible with libdatachannel, you can easily leverage both to make the same C++ code compile to native (including MacOS and Windows):

$ git submodule add https://github.com/paullouisageneau/datachannel-wasm.git deps/datachannel-wasm
$ git submodule add https://github.com/paullouisageneau/libdatachannel.git deps/libdatachannel
$ git submodule update --init --recursive

CMakeLists.txt:

if(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
    add_subdirectory(deps/datachannel-wasm EXCLUDE_FROM_ALL)
    target_link_libraries(YOUR_PROJECT datachannel-wasm)
else()
    option(NO_MEDIA "Disable media support in libdatachannel" ON)
    add_subdirectory(deps/libdatachannel EXCLUDE_FROM_ALL)
    target_link_libraries(YOUR_PROJECT datachannel-static)
endif()

Building

Building requires that you have emsdk installed and activated in your environment:

$ cmake -B build -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
$ cd build
$ make -j2