node-datachannel: WebRTC Data Channels for Node.js and Electron
The `node-datachannel` package provides Node.js and Electron bindings for the lightweight `libdatachannel` WebRTC data channel library. It offers a streamlined API for establishing and managing peer-to-peer data communication without the complexity of a full WebRTC stack implementation. Currently at version `0.32.2`, the project demonstrates an active release cadence, frequently incorporating updates from the underlying `libdatachannel` and introducing new features or fixes. Key differentiators include its small binary footprint (around 8MB for Linux x64), comprehensive TypeScript type definitions, and an integrated WebSocket client and server for flexible signaling. It specifically targets N-API version 8, requiring Node.js v18.20.0 or newer, and supports Linux, Windows, and macOS across various architectures. This makes it a suitable choice for applications needing robust, cross-platform WebRTC data channel capabilities in a server-side or Electron environment.
Common errors
-
Error: N-API version 8 is not supported by Node.js x.x.x
cause The installed `node-datachannel` binary was built for N-API v8 (Node.js >=18.20.0), but is being used with an incompatible Node.js version.fixUpgrade your Node.js runtime to version 18.20.0 or newer. For example, using `nvm install 18 && nvm use 18`. -
TypeError: Class constructor PeerConnection cannot be invoked without 'new'
cause The `PeerConnection` class is being called as a function instead of being instantiated with the `new` keyword.fixInstantiate `PeerConnection` using `new PeerConnection(...)` instead of `PeerConnection(...)`. -
Error: CMake 3.21 or greater is required.
cause When attempting to build `node-datachannel` from source, the system's installed CMake version is older than the minimum required version.fixUpdate your CMake installation to version 3.21 or newer. Check your current version with `cmake --version`. -
TypeError: Cannot read properties of undefined (reading 'PeerConnection')
cause This error often occurs when attempting to use CommonJS `require('node-datachannel').PeerConnection` with a module that primarily exports via ESM, or when the default export `nodeDataChannel` is not correctly accessed or destructured.fixIf using ESM, use `import { PeerConnection } from 'node-datachannel';`. If using CommonJS, try `const { PeerConnection } = require('node-datachannel');` or `const nodeDataChannel = require('node-datachannel'); const peer = new nodeDataChannel.PeerConnection(...)`.
Warnings
- breaking The `node-datachannel` package targets N-API version 8, which requires Node.js v18.20.0 or newer. Installing or running on older Node.js versions will likely lead to runtime errors or failed installation.
- breaking The arguments for `addIceCandidate` were updated in v0.25.0 to align with more standard WebRTC API signatures. Code written for older versions might pass incorrect arguments.
- gotcha For users building `node-datachannel` from source, CMake version 3.21 or greater is required. An older version will cause build failures.
- gotcha Prior to v0.30.0, there were several parameter and return type bugs in the `PeerConnection` wrapper, particularly affecting `RTCDataChannelInit` and `RTCDataChannelEvent` types, which could lead to unexpected behavior or TypeScript errors.
- gotcha In versions prior to v0.25.0, calling `RTCDataChannel.close()` could potentially block the event loop, causing performance degradation or unresponsiveness.
Install
-
npm install node-datachannel -
yarn add node-datachannel -
pnpm add node-datachannel
Imports
- nodeDataChannel
const nodeDataChannel = require('node-datachannel'); nodeDataChannel.initLogger('Debug');import nodeDataChannel from 'node-datachannel';
- PeerConnection
import nodeDataChannel from 'node-datachannel'; const peer = new nodeDataChannel.PeerConnection('Peer1'); // Less idiomaticimport { PeerConnection } from 'node-datachannel'; - initLogger
import nodeDataChannel from 'node-datachannel'; nodeDataChannel.initLogger('Debug');import { initLogger } from 'node-datachannel'; - WebSocket
const ws = new nodeDataChannel.WebSocket();
import { WebSocket } from 'node-datachannel';
Quickstart
import { PeerConnection, initLogger } from 'node-datachannel';
initLogger('Debug');
let dc1 = null;
let dc2 = null;
let peer1 = new PeerConnection('Peer1', {
iceServers: ['stun:stun.l.google.com:19302']
});
peer1.onLocalDescription((sdp, type) => {
peer2.setRemoteDescription(sdp, type);
});
peer1.onLocalCandidate((candidate, mid) => {
peer2.addRemoteCandidate(candidate, mid);
});
let peer2 = new PeerConnection('Peer2', {
iceServers: ['stun:stun.l.google.com:19302']
});
peer2.onLocalDescription((sdp, type) => {
peer1.setRemoteDescription(sdp, type);
});
peer2.onLocalCandidate((candidate, mid) => {
peer1.addRemoteCandidate(candidate, mid);
});
peer2.onDataChannel((dc) => {
dc2 = dc;
dc2.onMessage((msg) => {
console.log('Peer2 Received Msg:', msg);
});
dc2.sendMessage('Hello From Peer2');
});
dc1 = peer1.createDataChannel('test');
dc1.onOpen(() => {
dc1.sendMessage('Hello from Peer1');
});
dc1.onMessage((msg) => {
console.log('Peer1 Received Msg:', msg);
});