{"_id":"socketcluster-server@21.1.0","bugs":{"url":"https://github.com/SocketCluster/socketcluster-server/issues"},"dist":{"shasum":"6f4c5fded3c33e8fe072ebf2667c7744df74f55f","tarball":"http://registry-npm.ywwl.com/socketcluster-server/download/socketcluster-server-21.1.0.tgz","fileCount":8,"integrity":"sha512-v/Uh29RpTz7hGsPYwI0mvRBimjc1EnaPFTATa8Q7l9klgHuLgiV7YpVuB8pkdABmbvUokVH7CyR9Ns5FhH1okA==","signatures":[{"sig":"MEUCIDUBN/XQn8E/0wyw8vUkGmwTQ+4AVQx8GyFeti+1I6lBAiEAx2sXUDoRmNBErjx4hvnaBaE3da2prkvAtKEnmHNXEbo=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCQ0ydqv4QCgtpB+/3RGIRMfpnwTmgA1Qt1hdrEbjo1NgIgZxcOuixB6Srju9Ms55xcR1OOyZw8STto2F7qoFygb8o="}],"unpackedSize":213491,"size":31769,"noattachment":false,"key":"/socketcluster-server/-/socketcluster-server-21.1.0.tgz"},"main":"index.js","name":"socketcluster-server","author":{"name":"Jonathan Gros-Dubois","email":"grosjona@yahoo.com.au"},"gitHead":"12606560efc86a07072e9116ac91e888770181e0","license":"MIT","scripts":{"test":"mocha --reporter spec --timeout 10000 --slow 10000","start":"node server.js"},"version":"21.1.0","_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"homepage":"https://github.com/SocketCluster/socketcluster-server#readme","keywords":["websocket","realtime","socketcluster"],"overrides":{"diff":"^8.0.4","serialize-javascript":"^7.1.0"},"repository":{"url":"git://github.com/SocketCluster/socketcluster-server.git","type":"git"},"_npmVersion":"11.6.2","description":"Server module for SocketCluster","directories":{},"maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"_nodeVersion":"24.11.1","dependencies":{"ws":"^8.18.0","ag-auth":"^2.1.1","base64id":"^2.0.0","sc-errors":"^3.0.0","ag-request":"^1.1.0","clone-deep":"^4.0.1","sc-formatter":"^4.0.0","stream-demux":"^10.2.1","ag-simple-broker":"^6.1.1","async-stream-emitter":"^7.1.1","writable-consumable-stream":"^5.0.1"},"_hasShrinkwrap":false,"devDependencies":{"mocha":"^11.8.0","socketcluster-client":"^21.0.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/socketcluster-server_21.1.0_1793365227813_0.7129733802784007"},"readme":"# SocketCluster server\nMinimal server module for SocketCluster.\n\nThis is a stand-alone server module for SocketCluster.\nSocketCluster's protocol is backwards compatible with the SocketCluster protocol.\n\n## Setting up\n\nYou will need to install both ```socketcluster-server``` and ```socketcluster-client``` (https://github.com/SocketCluster/socketcluster-client).\n\nTo install this module:\n```bash\nnpm install socketcluster-server\n```\n\n## Usage\n\nYou need to attach it to an existing Node.js http or https server (example):\n```js\nconst 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        // ...\n      }\n    })();\n\n  }\n})();\n\nhttpServer.listen(8000);\n```\n\nFor more detailed examples of how to use SocketCluster, see `test/integration.js`.\nAlso, see tests from the `socketcluster-client` module.\n\nSocketCluster can work without the `for-await-of` loop; a `while` loop with `await` statements can be used instead.\nSee https://github.com/SocketCluster/stream-demux#usage\n\n## Compatibility mode\n\nFor compatibility with existing SocketCluster clients, set the `protocolVersion` to `1` and make sure that the `path` matches your old client path:\n\n```js\nlet agServer = socketClusterServer.attach(httpServer, {\n  protocolVersion: 1,\n  path: '/socketcluster/'\n});\n```\n\n## Maximum payload size\n\nSince version 21.0.0, the `maxPayload` option defaults to 4MB; before that it was unset, which left the `ws` engine default of 100MB in place. A message which exceeds the limit is rejected and the socket is closed with status code `1009`.\n\n```js\nlet agServer = socketClusterServer.attach(httpServer, {\n  maxPayload: 8 * 1024 * 1024\n});\n```\n\nSet it to `null` to fall back to the `wsEngine` default.\n\n## Maximum inbound batch size\n\nSince version 21.0.0, the `maxInboundBatchSize` option limits how many packets a single batched (array) message from a client may contain; it defaults to `1000`. A message which exceeds the limit is rejected and the socket is closed with status code `1009`.\n\n```js\nlet agServer = socketClusterServer.attach(httpServer, {\n  maxInboundBatchSize: 100\n});\n```\n\nSet it to `null` to allow batches of any size.\n\n## Socket channel limit\n\nThe `socketChannelLimit` option limits how many channels a single socket may be subscribed to at once; since version 21.0.3 it defaults to `1000`. A `#subscribe` request which would exceed the limit is rejected; the socket keeps its existing subscriptions and stays connected.\n\n```js\nlet agServer = socketClusterServer.attach(httpServer, {\n  socketChannelLimit: 100\n});\n```\n\nSet it to `null` to allow any number of subscriptions per socket. Note that each subscription costs memory on both the socket and the broker, so an unlimited setting lets a single client exhaust server memory; prefer a finite limit which suits your application.\n\n## Inbound backpressure limit\n\nThe `inboundBackpressureLimit` option limits how many unprocessed inbound messages a single socket may accumulate; since version 21.0.3 it defaults to `1000`. Inbound messages are processed one at a time (for example while asynchronous inbound middleware is applied), so a client which sends messages faster than the server can process them causes the backlog to grow. When the backlog exceeds the limit, the socket is closed with status code `1009`.\n\n```js\nlet agServer = socketClusterServer.attach(httpServer, {\n  inboundBackpressureLimit: 100\n});\n```\n\nSet it to `null` to allow an unbounded backlog. Since each queued message is held in memory, an unlimited setting lets a single client exhaust server memory; prefer a finite limit which suits your application. The current backlog for a socket can be inspected at runtime with `socket.getInboundBackpressure()`.\n\n## Running the tests\n\n- Clone this repo: `git clone git@github.com:SocketCluster/socketcluster-server.git`\n- Navigate to project directory: `cd socketcluster-server`\n- Install all dependencies: `npm install`\n- Run the tests: `npm test`\n\n## Benefits of async `Iterable` over `EventEmitter`\n\n- **More readable**: Code is written sequentially from top to bottom. It avoids event handler callback hell. It's also much easier to write and read complex integration test scenarios.\n- **More succinct**: Event streams can be easily chained, filtered and combined using a declarative syntax (e.g. using async generators).\n- **More manageable**: No need to remember to unbind listeners with `removeListener(...)`; just `break` out of the `for-await-of` loop to stop consuming. This also encourages a more declarative style of coding which reduces the likelihood of memory leaks and unintended side effects.\n- **Less error-prone**: Each event/RPC/message can be processed sequentially in the same order that they were sent without missing any data; even if asynchronous calls are made inside middleware or listeners. On the other hand, with `EventEmitter`, the listener function for the same event cannot be prevented from running multiple times in parallel; also, asynchronous calls within middleware and listeners can affect the final order of actions; all this can cause unintended side effects.\n\n## License\n\n(The MIT License)\n\nCopyright (c) 2013-2026 SocketCluster.io\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","publish_time":1793365227899,"_cnpm_publish_time":1793365227899,"dist-tags":{"latest":"21.1.0"}}