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.
npm install peerVerified import paths — ran on the pinned version, not inferred.
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.
Migrate your project to use ES module import syntax (`import ... from 'peer'`) and ensure your Node.js environment is configured for ESM (e.g., using `"type": "module"` in `package.json` or `.mjs` file extensions). Review `ws` v8 breaking changes if upgrading from older versions.
Consider running the PeerJS server on a non-privileged port (e.g., 9000 as in the quickstart) and use a reverse proxy like Nginx or Caddy to forward requests from standard HTTP/HTTPS ports. Ensure your proxy correctly handles WebSocket connections.
For production deployments, consider setting `allow_discovery: false` to disable ID listing. Implement your own secure mechanism for clients to exchange Peer IDs, such as a separate authentication service or direct sharing.
Add `"type": "module"` to your `package.json` file, or rename your server file to have a `.mjs` extension. Ensure all `require()` statements are converted to `import` statements.
Change the `PEER_PORT` environment variable or the hardcoded port in your server configuration to an available port. Alternatively, identify and terminate the process currently using that port (e.g., `lsof -i :9000` on Linux/macOS or `netstat -ano | findstr :9000` on Windows).
Verify that the `host`, `port`, and `path` parameters in your client-side `new Peer({...})` constructor exactly match the `PeerServer` configuration on your Node.js server. Ensure no firewalls are blocking connections to the specified port.