node-datachannel: WebRTC Data Channels for Node.js and Electron

0.32.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish two `PeerConnection` instances, exchange ICE candidates and SDP descriptions, and then create and communicate over a `DataChannel` between them, logging messages in both directions.

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);
});

view raw JSON →