Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Peer
✓ import { Peer } from 'protoo-client';
✗ import Peer from 'protoo-client';
The core `Peer` class is a named export. Ensure you provide a WebSocket instance (native browser WebSocket or `ws` for Node.js) when instantiating.
protoo-client in CJS
✓ const { Peer } = require('protoo-client');
✗ const protooClient = require('protoo-client');
const Peer = protooClient.Peer;
While the latter might work, destructuring directly is the idiomatic CommonJS way to access named exports. ESM is preferred for new projects.
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();
Errors
Common errors & fixes
peer.send is not a function
Attempting to use the deprecated `send()` method on a `Peer` instance from `protoo-client` version 4 or newer.
fixRename `peer.send()` to `peer.request()`. This was a breaking change in v4.
ReferenceError: WebSocket is not defined
This error typically occurs when running `protoo-client` in a Node.js environment without providing a WebSocket implementation. The browser's native `WebSocket` is not available in Node.js.
fixInstall the `ws` package (`npm install ws`) and import it. Then, pass an instance of `ws.WebSocket` to the `Peer` constructor, e.g., `new Peer(new WebSocket(url))`.
Error: Peer closed
This error often indicates that the underlying WebSocket connection was closed unexpectedly or failed to establish, and an operation (like `peer.request()`) was attempted on a disconnected peer.
fixEnsure the WebSocket URL is correct, the `protoo-server` is running and accessible, and handle `peer.on('close')` and `peer.on('error')` events to detect and react to disconnections. Re-establish the `Peer` and WebSocket connection if necessary. Audit
Dependencies
wsoptionalRequired for Node.js environments to provide a WebSocket implementation, as protoo-client does not bundle one.