protoo Server Module

4.0.8 · active · verified Sun Apr 19

protoo-server is a Node.js module that forms the server-side component of the protoo signaling framework. This framework is designed for building minimalist and extensible signaling layers for multi-party Real-Time Communication (RTC) applications, such as video conferencing or collaborative tools. Currently at version 4.0.8, the library maintains a steady release cadence, evidenced by its significant major version updates. It differentiates itself by providing a clear, event-driven API for managing WebSocket connections, peers, and rooms, allowing developers to focus on RTC logic rather than low-level WebSocket intricacies. The module facilitates the creation of `Room`s to organize connected `Peer`s, enabling efficient message routing and state management within a signaling context. Its focus on being "minimalist" means it provides core signaling primitives without enforcing complex architectural patterns.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic protoo-server instance using WebSocket over HTTP. It demonstrates how to handle incoming peer connections, process requests and notifications from connected peers, and respond to a simple 'echo' or 'joinRoom' request, illustrating a fundamental signaling flow.

import * as http from 'http';
import { WebSocketServer } from 'ws';
import Server from 'protoo-server';

const httpServer = http.createServer();
const wsServer = new WebSocketServer({ server: httpServer, noServer: true });

const protooServer = new Server();

wsServer.on('connection', (ws, request) => {
  const { url } = request;
  if (!url) {
    ws.close();
    return;
  }
  const peerId = url.split('/').pop() || `peer-${Date.now()}`;
  console.log(`New WebSocket connection from peer: ${peerId}`);

  let peer;
  try {
    peer = protooServer.createPeer(peerId, ws);
  } catch (error) {
    console.error(`Error creating protoo peer: ${error}`);
    ws.close();
    return;
  }

  peer.on('request', async (request, accept, reject) => {
    console.log(`Peer '${peer.id}' sent request:`, request);
    try {
      const response = await handlePeerRequest(peer, request);
      accept(response);
    } catch (error) {
      reject(error);
    }
  });

  peer.on('notification', (notification) => {
    console.log(`Peer '${peer.id}' sent notification:`, notification);
  });

  peer.on('close', () => {
    console.log(`Peer '${peer.id}' disconnected.`);
  });
});

httpServer.on('upgrade', (request, socket, head) => {
  wsServer.handleUpgrade(request, socket, head, (ws) => {
    wsServer.emit('connection', ws, request);
  });
});

async function handlePeerRequest(peer, request) {
  switch (request.method) {
    case 'echo':
      return { message: `Echoing: ${request.data.text}` };
    case 'joinRoom':
      // In a real app, you'd manage rooms here and add the peer
      console.log(`Peer '${peer.id}' wants to join room '${request.data.roomId}'`);
      peer.notify('roomJoined', { roomId: request.data.roomId, peers: [] });
      return { joined: true };
    default:
      throw new Error(`Unknown method: ${request.method}`);
  }
}

const PORT = process.env.PORT || 8080;
httpServer.listen(PORT, () => {
  console.log(`protoo-server listening on ws://localhost:${PORT}`);
  console.log('Connect with protoo-client: protoo-client.createPeer(wsUrl, { requestTimeout: 3000 })');
});

view raw JSON →