libdatachannel
Standalone WebRTC Data Channels, WebRTC Media Transport, and WebSockets
Wraps a native library — check OS Compatibility below for platform-specific linking notes.
Summary
| Latest Version | 0.2.0 |
|---|---|
| License | MIT |
| CI Status | Failing |
| Downloads | 0 |
| Last Indexed | 2026-11-21 14:03 |
Tags
Authors
- OpenPeeps
Installation
nimble install libdatachannel
choosenim install libdatachannel
git clone https://github.com/openpeeps/libdatachannel-nim
OS Compatibility
| Platform | Linux | macOS | Windows | FreeBSD | OpenBSD | NetBSD | Android | iOS | WASM | Embedded |
|---|---|---|---|---|---|---|---|---|---|---|
| libdatachannel | ✓ | ✓ | ✓ | - | - | - | - | - | - | - |
Dependencies
| Package | Version | Optional |
|---|---|---|
| nim >= | 2.0.0 | No |
Source
| Repository | https://github.com/openpeeps/libdatachannel-nim |
|---|---|
| Homepage | https://github.com/openpeeps/libdatachannel-nim |
| Registry Source | nimble_official |
README
Nim language 👑 bindings for Libdatachannel
A standalone WebRTC Data Channels, WebRTC Media Transport, and WebSockets
nimble install libdatachannel
😍 Key Features
- [x] Lightweight WebRTC Data Channel/Media Transport
-
[x] Fast Server/Client WebSockets
-
[x] High-level API in Nim style!
- [x] Low-level bindings
libdatachannel/bindings
Targets libdatachannel v0.24.x. Every entity of the C API is bound 1:1 in
bindings.nim, with the struct layouts
verified field-by-field against the real rtc.h.
Build the library
First, you will need to build libdatachannel from GitHub source. See Building instructions
The Nim bindings load the shared library at run time, so the C headers do not have to be installed, only the library itself has to be reachable by the dynamic loader:
LD_LIBRARY_PATH=/usr/local/lib nim c -r myapp.nim # Linux
DYLD_LIBRARY_PATH=/usr/local/lib nim c -r myapp.nim # macOS
Quick start
import std/[os]
import libdatachannel
# A WebSocket echo server.
let server = newWebSocketServer(Port(8080))
server.onClient = proc(client: WebSocketClient) =
client.onMessage = proc(data: string; isBinary: bool) =
client.send(data)
while true:
sleep(1000)
import std/[os]
import libdatachannel
# A WebSocket client.
let client = newWebSocketClient("ws://127.0.0.1:8080")
client.onOpen = proc() = client.send("Hello!")
client.onMessage = proc(data: string; isBinary: bool) = echo "<- ", data
while true:
sleep(1000)
Examples
Every example in examples/ is a single runnable file:
| Example | What it shows |
|---|---|
websocket_server.nim / websocket_client.nim |
WebSocket server and client, text and binary framing |
peer_connection.nim |
A complete data channel call between two PeerConnections in one process |
peer_connection_client.nim / peer_connection_server.nim |
The same call across two processes with manual signalling |
track_video.nim |
Media transport: VP8 packetizer, depacketizer and frames |
logger.nim |
Routing libdatachannel's log into std/logging |
# one command, no setup, no copy-pasting
nim c -r examples/peer_connection.nim
See examples/README.md for the full walkthrough.
WebSockets
# terminal 1
nim c -r examples/websocket_server.nim
# terminal 2
nim c -r examples/websocket_client.nim
Source of examples/websocket_server.nim
import std/os
import libdatachannel
const
BindAddress = "0.0.0.0"
ListenPort = 8080
proc onClient(client: WebSocketClient) =
echo "[+] client connected from ", client.remoteAddress()
# `onClient` fires as soon as the TCP connection is accepted, so anything
# that depends on the handshake has to wait for `onOpen`.
client.onOpen = proc() =
client.send("Welcome to the libdatachannel-nim echo server!")
client.onMessage = proc(data: string; isBinary: bool) =
# Echo it straight back, preserving the framing.
if isBinary:
client.sendBinary(data)
else:
client.send(data)
proc main() =
enableLogging()
let server = newWebSocketServer(WebSocketServerConfig(
port: Port(ListenPort),
bindAddress: BindAddress,
))
defer: server.close()
server.onClient = onClient
echo "WebSocket echo server listening on ws://", BindAddress, ":", server.port
while true:
sleep(1000)
main()
Source of examples/websocket_client.nim
import std/os
import libdatachannel
var finished = false
proc main(url: string) =
let client = newWebSocketClient(url)
defer: client.close()
# Callbacks run on libdatachannel's own threads, so they hand control back
# to the main loop through a flag instead of calling `quit` from there.
client.onOpen = proc() = client.send("Hello from Nim!")
client.onMessage = proc(data: string; isBinary: bool) =
let kind = if isBinary: "binary" else: "text"
echo "[<] ", data.len, " ", kind, " bytes: ", data
finished = true
client.onError = proc(message: string) =
stderr.writeLine("[!] ", message)
finished = true
while not finished:
sleep(50)
main("ws://127.0.0.1:8080")
Peer connection and data channel
nim c -r examples/peer_connection.nim
Source of examples/peer_connection.nim
import std/[os, times]
import libdatachannel
const IceServers = @["stun:stun.l.google.com:19302"]
proc waitUntil(condition: proc (): bool; timeoutMs = 30000): bool =
let deadline = epochTime() + float(timeoutMs) / 1000.0
while epochTime() < deadline:
if condition(): return true
sleep(20)
false
proc main() =
let caller = newPeerConnection(PeerConnectionConfig(iceServers: IceServers))
let callee = newPeerConnection()
defer: callee.close()
defer: caller.close()
# --- signaling: relay descriptions and candidates both ways ---------------
caller.onLocalDescription = proc(sdp: string; kind: SdpType) =
callee.setRemoteDescription(sdp, kind)
callee.onLocalDescription = proc(sdp: string; kind: SdpType) =
caller.setRemoteDescription(sdp, kind)
caller.onLocalCandidate = proc(candidate, mid: string) =
if candidate.len > 0: callee.addRemoteCandidate(candidate, mid)
callee.onLocalCandidate = proc(candidate, mid: string) =
if candidate.len > 0: caller.addRemoteCandidate(candidate, mid)
# --- the caller's side of the channel --------------------------------------
var dc: DataChannel
callee.onDataChannel = proc(channel: DataChannel) =
echo "[callee] channel '", channel.label(), "' opened"
channel.onMessage = proc(data: string; isBinary: bool) =
echo "[callee] <- ", data
channel.send("echo: " & data)
caller.onStateChange = proc(state: rtcState) = echo "[caller] ", state
dc = caller.createDataChannel("chat")
dc.onOpen = proc() =
echo "[caller] sending"
dc.send("Hello from the caller!")
# A PeerConnection has nothing to negotiate without a data channel or a
# track, which is exactly why `createDataChannel` comes first.
caller.setLocalDescription()
doAssert waitUntil(proc(): bool = dc.isOpen)
sleep(500)
main()
Media transport
nim c -r examples/track_video.nim
Source of the media parts of examples/track_video.nim
# --- receiving side --------------------------------------------------------
receiver.onTrack = proc(track: Track) =
# Incoming media arrives as raw payloads on the message callback; the C
# API has no depacketizer or frame callback to reassemble frames.
track.onMessage = proc(data: string; isBinary: bool) =
echo "payload of ", data.len, " bytes"
track.chainRtcpReceivingSession()
# --- sending side ----------------------------------------------------------
# The track has to exist before the offer is created, otherwise the media
# section is missing from the SDP entirely. H264 has a packetizer, so
# frames go out as RTP packets with a drivable timestamp.
let track = sender.addTrack(RTC_CODEC_H264,
PacketizerInit(clockRate: 90000, payloadType: 102))
sender.setLocalDescription()
track.send(frame)
API overview
PeerConnection
let pc = newPeerConnection(PeerConnectionConfig(
iceServers: @["stun:stun.l.google.com:19302"],
bindAddress: "0.0.0.0",
certificateType: RTC_CERTIFICATE_ECDSA,
portRangeBegin: 50000,
portRangeEnd: 50100,
mtu: 1280,
maxMessageSize: 262144,
))
pc.onLocalDescription = proc(sdp: string; kind: SdpType) = discard
pc.onLocalCandidate = proc(candidate, mid: string) = discard
pc.onStateChange = proc(state: rtcState) = discard
pc.onIceStateChange = proc(state: rtcIceState) = discard
pc.onGatheringStateChange = proc(state: rtcGatheringState) = discard
pc.onSignalingStateChange = proc(state: rtcSignalingState) = discard
pc.onDataChannel = proc(dc: DataChannel) = discard
pc.onTrack = proc(tr: Track) = discard
pc.setLocalDescription() # offer or answer
pc.setRemoteDescription(sdp, sdpOffer)
pc.addRemoteCandidate(candidate, mid)
pc.localDescription(); pc.remoteDescription()
pc.localAddress(); pc.remoteAddress()
pc.selectedCandidatePair()
pc.isNegotiationNeeded()
pc.close()
DataChannel
let dc = pc.createDataChannel("chat", DataChannelInit(
reliability: Reliability(unordered: true, unreliable: true, maxRetransmits: 3),
protocol: "json",
))
dc.onOpen = proc() = dc.send("ready")
dc.onMessage = proc(data: string; isBinary: bool) = discard
dc.onClosed = proc() = discard
dc.onError = proc(message: string) = discard
dc.send("text message")
dc.sendBinary(bytes) # openArray[byte], verbatim
dc.label(); dc.protocol(); dc.stream(); dc.reliability()
dc.bufferedAmount(); dc.availableAmount(); dc.maxMessageSize()
dc.close()
Track
let track = pc.addTrack(RTC_CODEC_OPUS,
PacketizerInit(clockRate: 48000, payloadType: 111, cname: "mic"))
# Packetizer setters exist for H264/H265/AV1/Opus/AAC/PCMU/PCMA/G722 only;
# VP8/VP9 tracks send raw payloads, read back through `onMessage`.
track.onMessage = proc(data: string; isBinary: bool) = discard
track.send(frame)
track.setRtpTimestamp(90000)
track.requestKeyframe()
track.requestBitrate(500_000)
track.description(); track.mid(); track.direction()
track.ssrcs(); track.payloadTypesForCodec("opus"); track.cName(ssrc)
track.secondsToTimestamp(0.020); track.timestampToSeconds(timestamp)
track.chainRtcpReceivingSession()
track.chainRtcpSrReporter()
track.chainRtcpNackResponder()
track.chainPliHandler(callback)
track.chainRembHandler(callback)
WebSocketServer / WebSocketClient
let server = newWebSocketServer(WebSocketServerConfig(
port: Port(8080),
enableTls: false, # wss://
certificatePemFile: "cert.pem",
keyPemFile: "key.pem",
bindAddress: "0.0.0.0",
))
server.onClient = proc(client: WebSocketClient) =
client.onOpen = proc() = client.path()
client.onMessage = proc(data: string; isBinary: bool) = client.send(data)
client.onClosed = proc() = discard
client.onError = proc(message: string) = discard
server.port
server.close()
let client = newWebSocketClient(WebSocketClientConfig(
disableTlsVerification: false,
protocols: @["chat"],
pingIntervalMs: 5000,
))
client.remoteAddress(); client.path()
client.close()
Logging
enableLogging() # info level to stderr
enableLogging(RTC_LOG_DEBUG)
setLogLevel(RTC_LOG_NONE)
setLogger(proc(level: rtcLogLevel; message: string) = echo level, message)
Global settings
discard rtcPreload() # warm the library up, returns true if it was not up
rtcSetThreadPoolSize(4)
rtcSetSctpSettings(addr settings)
rtcCleanup() # release everything, at the very end
Low-level bindings
libdatachannel/bindings is a straight 1:1 mapping of rtc.h, so anything
the high-level API does not cover is still available:
import libdatachannel/bindings
var config = rtcConfiguration(enableIceTcp: true, mtu: 1280)
let pc = rtcCreatePeerConnection(addr config)
rtcSetMessageCallback(pc, myCdeclCallback)
Gotchas worth knowing
- Negotiate something.
setLocalDescriptionfails withRTC_ERR_FAILURE("No DataChannel or Track to negotiate") unless a DataChannel or Track was created first. - A DataChannel or Track needs a packetizer. libdatachannel rejects a
NULLCNAME, soPacketizerInit.cnamefalls back todefaultPacketizerCname. - Only
chainRtcpReceivingSessionworks on a receiving track. The sender reporter needs an RTP configuration, which only the sending side's packetizer creates. onClientfires before the handshake. A WebSocket server hands you the client as soon as TCP is accepted; readpath()fromonOpen.- Empty strings mean "unset". Optional
cstringfields in the C configuration structs are passed asnil, never as a pointer to"", otherwise libdatachannel tries to load an empty certificate file. close()everything. Channels, tracks, peer connections and servers hold native resources until they are closed.- Callbacks run on libdatachannel's threads. They never let a Nim exception escape into C++, and libdatachannel-nim keeps the objects alive for as long as the library may call back into them.
- A callback that captures its own channel forms a reference cycle
(
client.onMessage = proc(...) = client.send(...)), which Nim reports asCycleCreated. It is not a leak:close()drops the handlers and breaks the cycle, so as long as every channel is closed the memory is reclaimed. - Memory manager. Build with
--mm:arc(or--gc:refc). Nim's default--mm:orccorrupts its heap when Nim code allocates from threads the Nim runtime did not create, which is exactly what a WebRTC callback is. Under ORC the test suite dies withSIGSEGV: Illegal storage access, sotests/config.nimspinsswitch("mm", "arc")and bothclue testandnim c --path:src -r tests/test_*.nimpick it up automatically. An explicit--mmon the command line still wins.
❤ Contributions & Support
- 🐛 Found a bug? Create a new Issue
- 👋 Wanna help? Fork it!
🎩 License
MIT license. Made by Humans from OpenPeeps.
Copyright © 2025 OpenPeeps & Contributors — All rights reserved.