WebRTC data channels for Node.js with the same RTCPeerConnection and
RTCDataChannel API as browsers. Built on
libdatachannel, shipped
as a Node-API addon with prebuilt binaries, and validated against 620 selected
Web Platform Tests subtests on Linux, macOS,
and Windows.
npm install @webrtc-node/webrtcconst { RTCPeerConnection } = require("@webrtc-node/webrtc");
const alice = new RTCPeerConnection();
const bob = new RTCPeerConnection();
// Exchange ICE candidates. In a real app these go through your signaling server.
alice.onicecandidate = ({ candidate }) => candidate && bob.addIceCandidate(candidate);
bob.onicecandidate = ({ candidate }) => candidate && alice.addIceCandidate(candidate);
bob.ondatachannel = ({ channel }) => {
channel.onmessage = ({ data }) => {
console.log(`bob received: ${data}`);
alice.close();
bob.close();
};
};
const channel = alice.createDataChannel("chat");
channel.onopen = () => channel.send("hello from Node.js");
(async () => {
await alice.setLocalDescription();
await bob.setRemoteDescription(alice.localDescription);
await bob.setLocalDescription();
await alice.setRemoteDescription(bob.localDescription);
})();More in examples/: peers in separate processes, and streaming with backpressure.
- Browser-compatible API. Code written against browser
RTCPeerConnectionandRTCDataChannelruns unchanged, including events,bufferedAmount, and DOMException errors. Interoperability with Chrome is tested in CI. - Event loop stays free. SCTP, DTLS, and socket work runs on native threads, so heavy sending does not block your application.
- Installs without scripts. Prebuilt binaries for eight targets ship as per-platform npm packages, so installation works where package managers block install scripts. ESM, CommonJS, and TypeScript declarations are included.
- Focused extensions for servers, such as ICE UDP multiplexing on one port
and PEM certificate import, in a separate
nonstandardnamespace.
The scope is peer connections and data channels. Media tracks, transceivers, and statistics are not supported.
Node.js 20 or newer.
| OS | Prebuilt targets | Node.js |
|---|---|---|
| Linux | x64 glibc, x64 musl, arm64 glibc, arm64 musl | 20, 22, 24, 26 |
| macOS | x64, arm64 | 20, 22, 24, 26 |
| Windows | x64, arm64 | 20, 22, 24, 26 |
The package manager installs the matching binary as an optional dependency,
such as @webrtc-node/webrtc-linux-x64-glibc, and no install script runs.
For other platforms, build from source as described in
docs/development.md.
Compared with node-datachannel, sending between two Node.js processes on one Windows desktop:
@webrtc-node/webrtc |
node-datachannel |
|
|---|---|---|
| Small messages per second | 44,200 | 40,500 |
| CPU per small message sent | 45 µs | 51 µs |
| Sender's main thread busy | 4% | 100% |
| Large transfers | 65 MiB/s | 65 MiB/s |
| Round-trip time | 0.13 ms | 0.12 ms |
| CPU per round trip | 128 µs | 100 µs |
To run the comparison yourself:
npm run benchmark:compare -- --library=webrtc-node --scenario=rate
npm install --no-save node-datachannel
npm run benchmark:compare -- --library=node-datachannel --scenario=rateScenarios are rate, text, bulk, and pingpong. Results depend on the
machine and workload.
- Architecture: native threading, send backlog, and event dispatch
- Conformance: the selected WPT suite and how it runs
- Divergences: where behavior intentionally differs from browsers
- Nonstandard extensions: libdatachannel-specific APIs
- Development: building, testing, and benchmarking
- Changelog
See CONTRIBUTING.md. Report security issues privately as described in SECURITY.md.