{"id":11414,"library":"node-datachannel","title":"node-datachannel: WebRTC Data Channels for Node.js and Electron","description":"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.","status":"active","version":"0.32.2","language":"javascript","source_language":"en","source_url":"https://github.com/murat-dogan/node-datachannel","tags":["javascript","libdatachannel","webrtc","p2p","peer-to-peer","datachannel","data channel","websocket","typescript"],"install":[{"cmd":"npm install node-datachannel","lang":"bash","label":"npm"},{"cmd":"yarn add node-datachannel","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-datachannel","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary import is a default export, often aliased to `nodeDataChannel`. CommonJS `require` users should access named exports directly or use `.default` where applicable.","wrong":"const nodeDataChannel = require('node-datachannel');\nnodeDataChannel.initLogger('Debug');","symbol":"nodeDataChannel","correct":"import nodeDataChannel from 'node-datachannel';"},{"note":"`PeerConnection` is a named export. It is idiomatic to destructure it from the module for direct use.","wrong":"import nodeDataChannel from 'node-datachannel';\nconst peer = new nodeDataChannel.PeerConnection('Peer1'); // Less idiomatic","symbol":"PeerConnection","correct":"import { PeerConnection } from 'node-datachannel';"},{"note":"`initLogger` is a named export for configuring the library's logging level.","wrong":"import nodeDataChannel from 'node-datachannel';\nnodeDataChannel.initLogger('Debug');","symbol":"initLogger","correct":"import { initLogger } from 'node-datachannel';"},{"note":"The integrated WebSocket client is available as a named export. Ensure it's destructured or accessed correctly.","wrong":"const ws = new nodeDataChannel.WebSocket();","symbol":"WebSocket","correct":"import { WebSocket } from 'node-datachannel';"}],"quickstart":{"code":"import { PeerConnection, initLogger } from 'node-datachannel';\n\ninitLogger('Debug');\n\nlet dc1 = null;\nlet dc2 = null;\n\nlet peer1 = new PeerConnection('Peer1', {\n  iceServers: ['stun:stun.l.google.com:19302']\n});\n\npeer1.onLocalDescription((sdp, type) => {\n  peer2.setRemoteDescription(sdp, type);\n});\npeer1.onLocalCandidate((candidate, mid) => {\n  peer2.addRemoteCandidate(candidate, mid);\n});\n\nlet peer2 = new PeerConnection('Peer2', {\n  iceServers: ['stun:stun.l.google.com:19302']\n});\n\npeer2.onLocalDescription((sdp, type) => {\n  peer1.setRemoteDescription(sdp, type);\n});\npeer2.onLocalCandidate((candidate, mid) => {\n  peer1.addRemoteCandidate(candidate, mid);\n});\npeer2.onDataChannel((dc) => {\n  dc2 = dc;\n  dc2.onMessage((msg) => {\n    console.log('Peer2 Received Msg:', msg);\n  });\n  dc2.sendMessage('Hello From Peer2');\n});\n\ndc1 = peer1.createDataChannel('test');\n\ndc1.onOpen(() => {\n  dc1.sendMessage('Hello from Peer1');\n});\n\ndc1.onMessage((msg) => {\n  console.log('Peer1 Received Msg:', msg);\n});","lang":"typescript","description":"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."},"warnings":[{"fix":"Ensure your Node.js environment is at least v18.20.0. Update using `nvm install 18 && nvm use 18` or similar version management tools.","message":"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.","severity":"breaking","affected_versions":"<=0.28.0 (prior to strict enforcement/N-API v8 targeting)"},{"fix":"Review calls to `addIceCandidate` and ensure they conform to the updated signature, typically expecting an `RTCIceCandidate` object and a `mid` string.","message":"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.","severity":"breaking","affected_versions":">=0.25.0"},{"fix":"Upgrade your CMake installation to version 3.21 or newer before attempting to build the package. Check `cmake --version`.","message":"For users building `node-datachannel` from source, CMake version 3.21 or greater is required. An older version will cause build failures.","severity":"gotcha","affected_versions":">=0.29.0"},{"fix":"Update to v0.30.0 or newer to benefit from fixes to these type and mapping issues, ensuring more reliable API interaction.","message":"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.","severity":"gotcha","affected_versions":"<0.30.0"},{"fix":"Upgrade to v0.25.0 or a later version to use the non-blocking `close()` implementation.","message":"In versions prior to v0.25.0, calling `RTCDataChannel.close()` could potentially block the event loop, causing performance degradation or unresponsiveness.","severity":"gotcha","affected_versions":"<0.25.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Upgrade your Node.js runtime to version 18.20.0 or newer. For example, using `nvm install 18 && nvm use 18`.","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.","error":"Error: N-API version 8 is not supported by Node.js x.x.x"},{"fix":"Instantiate `PeerConnection` using `new PeerConnection(...)` instead of `PeerConnection(...)`.","cause":"The `PeerConnection` class is being called as a function instead of being instantiated with the `new` keyword.","error":"TypeError: Class constructor PeerConnection cannot be invoked without 'new'"},{"fix":"Update your CMake installation to version 3.21 or newer. Check your current version with `cmake --version`.","cause":"When attempting to build `node-datachannel` from source, the system's installed CMake version is older than the minimum required version.","error":"Error: CMake 3.21 or greater is required."},{"fix":"If 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(...)`.","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.","error":"TypeError: Cannot read properties of undefined (reading 'PeerConnection')"}],"ecosystem":"npm"}