{"library":"peer","title":"PeerJS Server Component","description":"The `peer` package provides the server-side component for PeerJS, a WebRTC signaling library, enabling seamless peer-to-peer connections in web applications. It serves as the intermediary for PeerJS clients to discover each other and exchange crucial connection information before establishing a direct WebRTC link. The current stable version is 1.0.2, with active development progressing towards v1.1.0, evidenced by recent release candidates. This project maintains a steady release cadence, primarily focusing on dependency updates, bug fixes, and minor enhancements. Its key differentiator is its tight integration and compatibility with the PeerJS client library, offering a straightforward solution for deploying a WebRTC signaling server without requiring complex custom implementations. It also supports integration with existing Express applications, providing flexibility for deployment.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install peer"],"cli":{"name":"peer","version":null}},"imports":["import { PeerServer } from 'peer';","import { ServerOptions } from 'peer';","import express, { Application } from 'express';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { PeerServer } from 'peer';\nimport express from 'express';\nimport { createServer } from 'http';\nimport path from 'path';\n\n// Define the port and path for the PeerJS server, allowing environment variable overrides.\nconst PEER_PORT = process.env.PEER_PORT ? parseInt(process.env.PEER_PORT, 10) : 9000;\nconst PEER_PATH = process.env.PEER_PATH || '/myapp'; // Use a distinct path for isolation\n\n// Create an Express application to serve static files or handle other API routes.\nconst app = express();\n\n// Serve a basic static HTML file from a 'public' directory.\n// Make sure to create a 'public' directory with an 'index.html' for this example to run fully.\napp.use(express.static(path.join(__dirname, '../public')));\n\n// Create an HTTP server that will host both the Express app and the PeerJS WebSocket server.\nconst httpServer = createServer(app);\n\n// Initialize PeerJS server and attach it to the existing HTTP server.\n// The `PeerServer` function creates an instance that listens for WebSocket connections.\nconst peerServer = PeerServer({\n  port: PEER_PORT,\n  path: PEER_PATH,\n  allow_discovery: true, // Enables client discovery (optional, security consideration)\n  // For production, consider adding key and cert for HTTPS\n  // key: fs.readFileSync('path/to/key.pem'),\n  // cert: fs.readFileSync('path/to/cert.pem'),\n}, (server) => {\n  console.log(`PeerJS server initialized.`);\n  console.log(`  - WebSocket endpoint: ws://localhost:${PEER_PORT}${PEER_PATH}`);\n  console.log(`  - Server ID: ${server.id}`);\n});\n\n// Listen on the specified port for HTTP and WebSocket connections.\nhttpServer.listen(PEER_PORT, () => {\n  console.log(`HTTP server (for static content) listening on port ${PEER_PORT}`);\n  console.log('Access the client at http://localhost:9000/index.html');\n});\n\n// Handle server errors\nhttpServer.on('error', (err: NodeJS.ErrnoException) => {\n  if (err.code === 'EADDRINUSE') {\n    console.error(`Port ${PEER_PORT} is already in use. Please choose another port or terminate the existing process.`);\n  } else {\n    console.error(`Server error: ${err.message}`);\n  }\n  process.exit(1);\n});\n\n// Graceful shutdown\nprocess.on('SIGINT', () => {\n  console.log('Shutting down server...');\n  peerServer.destroy(() => {\n    httpServer.close(() => {\n      console.log('Server gracefully shut down.');\n      process.exit(0);\n    });\n  });\n});","lang":"typescript","description":"This quickstart demonstrates how to set up and run the PeerJS server using TypeScript, integrating it with an Express application to serve static content. It includes configuration for port and path via environment variables, basic error handling for port conflicts, and graceful shutdown.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}