{"library":"protoo-server","title":"protoo Server Module","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install protoo-server"],"cli":null},"imports":["import Server from 'protoo-server';","import { Room, Peer } from 'protoo-server';","const protoo = require('protoo-server');\nconst Server = protoo.default;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import * as http from 'http';\nimport { WebSocketServer } from 'ws';\nimport Server from 'protoo-server';\n\nconst httpServer = http.createServer();\nconst wsServer = new WebSocketServer({ server: httpServer, noServer: true });\n\nconst protooServer = new Server();\n\nwsServer.on('connection', (ws, request) => {\n  const { url } = request;\n  if (!url) {\n    ws.close();\n    return;\n  }\n  const peerId = url.split('/').pop() || `peer-${Date.now()}`;\n  console.log(`New WebSocket connection from peer: ${peerId}`);\n\n  let peer;\n  try {\n    peer = protooServer.createPeer(peerId, ws);\n  } catch (error) {\n    console.error(`Error creating protoo peer: ${error}`);\n    ws.close();\n    return;\n  }\n\n  peer.on('request', async (request, accept, reject) => {\n    console.log(`Peer '${peer.id}' sent request:`, request);\n    try {\n      const response = await handlePeerRequest(peer, request);\n      accept(response);\n    } catch (error) {\n      reject(error);\n    }\n  });\n\n  peer.on('notification', (notification) => {\n    console.log(`Peer '${peer.id}' sent notification:`, notification);\n  });\n\n  peer.on('close', () => {\n    console.log(`Peer '${peer.id}' disconnected.`);\n  });\n});\n\nhttpServer.on('upgrade', (request, socket, head) => {\n  wsServer.handleUpgrade(request, socket, head, (ws) => {\n    wsServer.emit('connection', ws, request);\n  });\n});\n\nasync function handlePeerRequest(peer, request) {\n  switch (request.method) {\n    case 'echo':\n      return { message: `Echoing: ${request.data.text}` };\n    case 'joinRoom':\n      // In a real app, you'd manage rooms here and add the peer\n      console.log(`Peer '${peer.id}' wants to join room '${request.data.roomId}'`);\n      peer.notify('roomJoined', { roomId: request.data.roomId, peers: [] });\n      return { joined: true };\n    default:\n      throw new Error(`Unknown method: ${request.method}`);\n  }\n}\n\nconst PORT = process.env.PORT || 8080;\nhttpServer.listen(PORT, () => {\n  console.log(`protoo-server listening on ws://localhost:${PORT}`);\n  console.log('Connect with protoo-client: protoo-client.createPeer(wsUrl, { requestTimeout: 3000 })');\n});\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}