@leede/webrtc-server
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

WebRTC Server

The package provides a Node.js server that acts as a central peer for WebRTC connections. This is beneficial in use-cases where the reliable and ordered messaging of the WebSocket protocol is a limiting factor, such as web-based multiplayer games. The server sets up two data channels for each incoming connection - one for reliable messaging and another one for unreliable messaging. This allows for applications to use a unified messaging mechanism to implement features that require either reliable transports such as a chat room or unreliable transports such as syncing game state.

Check out the live demo or the documentation.

Server installation

npm install @leede/webrtc-server

Basic server usage example

import { WebRTCServer } from "@leede/webrtc-server";

const server = new WebRTCServer({
  port: 8000,
  iceServers: ["stun:stun.l.google.com:19302"],
});

server.on("connection", (connection) => {
  console.log("[SERVER] New connection");

  // Send reliable messages
  connection.sendR("Hello from server over TCP");
  connection.sendR(new Float32Array([1.618, 1.414]).buffer);

  // Send unreliable messages
  connection.sendU("Hello from server over UDP");
  connection.sendU(Buffer.from([1, 4, 9, 16, 25, 36]));

  // Handle string messages from connection
  connection.on("message", (message) => {
    console.log("[SERVER] Received message:", message);
  });

  // Handle binary messages from connection
  connection.on("binary", (buffer) => {
    console.log("[SERVER] Received buffer:", buffer);
  });

  // Handle disconnection
  connection.on("close", () => {
    console.log("Connection closed");
  });
});

For detailed usage, see the server documentation.

Package Sidebar

Install

npm i @leede/webrtc-server

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

18.5 kB

Total Files

11

Last publish

Collaborators

  • gutnar