SocketCluster Server (version 20.0.0) is the core server-side module for the SocketCluster real-time framework, designed to facilitate highly scalable, event-driven applications using WebSockets. It operates by attaching to an existing Node.js HTTP/HTTPS server and provides robust mechanisms for managing inbound connections, handling Remote Procedure Calls (RPCs), and processing real-time event streams. A key differentiator and architectural shift in recent major versions is its adoption of modern JavaScript async iterators (`for-await-of` loops) for stream processing, moving away from traditional `EventEmitter` patterns. This paradigm promotes more readable, succinct, and less error-prone code by reducing callback hell and simplifying resource management, as listeners do not need explicit unbinding. While it maintains a compatibility mode for older clients (protocolVersion 1), the current stable version encourages the use of its async iterable API. Releases tend to align with major Node.js LTS updates or significant architectural improvements.
npm install socketcluster-serverVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to attach SocketCluster server to an HTTP server and handle inbound connections, RPC procedures, and remote events using async iterators. It also shows transmitting a basic event to connected clients.
Migrate event handling logic to use `for await (const { socket } of agServer.listener('connection'))` and `for await (let req of socket.procedure('procName'))` patterns. Refer to the official v16.x documentation on socketcluster.io.Configure `agServer` attachment with compatibility options: `let agServer = socketClusterServer.attach(httpServer, { protocolVersion: 1, path: '/socketcluster/' });`Install both packages: `npm install socketcluster-server socketcluster-client` and refer to `socketcluster-client` documentation for client-side setup.
Use a recent LTS version of Node.js (e.g., Node.js 18 or 20) to ensure full compatibility with the async iterator paradigm and performance benefits.
If running in an ES Module environment (e.g., `"type": "module"` in `package.json`), change import statements to `import * as socketClusterServer from 'socketcluster-server';` or configure your build tool for CommonJS compatibility.
Enable compatibility mode on the server: `socketClusterServer.attach(httpServer, { protocolVersion: 1 });` or update your client to use a compatible `socketcluster-client` version and its default protocol.Ensure `agServer` is correctly created using `socketClusterServer.attach(httpServer);` and that you are using the `listener()` method as shown in the v16+ documentation for async iterators.
Change the `httpServer.listen()` port to an available one, or ensure no other applications are using the desired port. On Linux, `lsof -i :8000` can identify the process to terminate.