protoo-client

4.0.8 · active · verified Wed Apr 22

protoo-client is the client-side JavaScript library for the protoo signaling framework, designed for building multi-party Real-Time Communication (RTC) applications. It supports both browser and Node.js environments, requiring a compatible WebSocket implementation (native `WebSocket` in browsers, or a library like `ws` in Node.js). The current stable version is 4.0.8, and the project appears to follow semantic versioning with notable breaking changes between major releases, indicating active development. It differentiates itself by offering a minimalist and extensible approach to signaling, focusing specifically on WebRTC use cases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a protoo-client Peer, establish a WebSocket connection, and handle basic 'open', 'close', 'error', 'request', and 'notification' events. It distinguishes between Node.js and browser WebSocket usage.

import { Peer } from 'protoo-client';
import { WebSocket } from 'ws'; // Only for Node.js. In browsers, use native WebSocket.

const WEBSOCKET_URL = process.env.PROTOO_SERVER_URL || 'wss://your.protoo.server:4443';

async function connectProtoo() {
  let ws;
  if (typeof window === 'undefined') {
    // Node.js environment
    ws = new WebSocket(WEBSOCKET_URL);
  } else {
    // Browser environment
    ws = new WebSocket(WEBSOCKET_URL);
  }

  const peer = new Peer(ws);

  peer.on('open', () => {
    console.log('Protoo Peer connected to server!');
    // Example: Send an initial request
    peer.request('hello', { message: 'Hello from client!' })
      .then(response => console.log('Server response:', response))
      .catch(error => console.error('Request failed:', error));
  });

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

  peer.on('error', (error) => {
    console.error('Protoo Peer error:', error.message);
  });

  peer.on('request', async (request, accept, reject) => {
    console.log('Received request from server:', request.method, request.data);
    try {
      if (request.method === 'ping') {
        accept({ pong: 'received' });
      } else {
        reject(new Error('Unknown request method'));
      }
    } catch (error) {
      reject(error);
    }
  });

  peer.on('notification', (notification) => {
    console.log('Received notification from server:', notification.method, notification.data);
  });
}

connectProtoo();

view raw JSON →