{"library":"socketcluster-server","title":"SocketCluster Server","type":"library","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install socketcluster-server"],"cli":null},"imports":["const socketClusterServer = require('socketcluster-server');","import type { AGServer } from 'socketcluster-server';","import type { AGSocket } from 'socketcluster-server';"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":"https://socketcluster.io/","github":"https://github.com/SocketCluster/socketcluster-server","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/socketcluster-server","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"const http = require('http');\nconst socketClusterServer = require('socketcluster-server');\n\nlet httpServer = http.createServer();\nlet agServer = socketClusterServer.attach(httpServer);\n\n(async () => {\n  // Handle new inbound sockets.\n  for await (let {socket} of agServer.listener('connection')) {\n\n    (async () => {\n      // Set up a loop to handle and respond to RPCs for a procedure.\n      for await (let req of socket.procedure('customProc')) {\n        if (!req.data) {\n          let error = new Error('Server failed to execute the procedure');\n          error.name = 'BadCustomError';\n          req.error(error);\n        } else {\n          req.end('Success');\n        }\n      }\n    })();\n\n    (async () => {\n      // Set up a loop to handle remote transmitted events.\n      for await (let data of socket.receiver('customRemoteEvent')) {\n        console.log(`Received customRemoteEvent: ${data}`);\n      }\n    })();\n\n    socket.transmit('helloClient', { message: 'Hello from server!' });\n  }\n})();\n\nhttpServer.listen(8000, () => {\n  console.log('SocketCluster Server listening on port 8000');\n});","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}