{"_id":"@dataxquare/sse-pubsub","_rev":"2-d88b7858c304430c66b1781e52cccb97","name":"@dataxquare/sse-pubsub","dist-tags":{"latest":"1.5.1"},"versions":{"1.5.0":{"name":"@dataxquare/sse-pubsub","version":"1.5.0","description":"Server-sent events with pub/sub interface","main":"index.js","scripts":{"test":"mocha test/tests.mjs --exit","start":"node demo/server.js","publish":"npm publish --access public"},"repository":{"type":"git","url":"git+ssh://git@github.com/triblondon/node-sse-pubsub.git"},"keywords":["sse","http","server","pubsub"],"author":{"name":"Andrew Betts","email":"andrew@trib.tv"},"license":"MIT","bugs":{"url":"https://github.com/triblondon/node-sse-pubsub/issues"},"homepage":"https://github.com/triblondon/node-sse-pubsub#readme","devDependencies":{"chai":"^4.3.4","express":"^4.17.1","mocha":"^9.1.3","node-fetch":"^3.0.0"},"gitHead":"ae13d9ffa6a61bd74b3de0b634b8c34dc972c1aa","_id":"@dataxquare/sse-pubsub@1.5.0","_nodeVersion":"14.16.1","_npmVersion":"7.13.0","dist":{"integrity":"sha512-pLh1fAH3WMI/xceNGibx6YCg2paPjmh5g0EsY0zjZa5iIWr0OCWPmQN+ZAwDmpgBRP0igvtcZ796bDUjFVg5dw==","shasum":"785ea245d86948e001b66fee33833fbb0cd0a5fe","tarball":"https://registry.npmjs.org/@dataxquare/sse-pubsub/-/sse-pubsub-1.5.0.tgz","fileCount":8,"unpackedSize":150971,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh1cwlCRA9TVsSAnZWagAAMlsQAIfBlVfJSKLj1lDvoPB3\nHPCSIREaLZ6GkmCQLnavPuO2uxmQKShJo03bTTMO6XALJ6y1qJROoDcLWv26\n33XNKNly0jMcGiimCTAVxcGfQSDxtCT5W3t3Cc4Uut0s4lNxa+7RsfZ6Ku9E\nbqoxmoUj+5qKykXLRH/TIDk6GlnufNotb7h9d34dX1i+9Qc75Rd4kacUfM8M\nmIEwcjJsgzFJDOt2tngZ5p9+KzvsV0+j3feQMtt4z7IXj/StCTbFx9SiQY9I\nWboKgfklyv++jkd0mApWDdjjgL1tGhRRMt4zat/Jigm0WwPMu5Rwx1MHGAgK\ndSkI6rNH98pkfhZIwStY9nGD7cuXnCINlDl3hRmeYs5lSCMTHxkhr9OWv78W\njJeKTL57DX9sG5BCfC7/wktRQGspsoF9R/4SWGFq5VjhbR1w++ojzHUobIMx\nsVj9LwlHX/Y3FtT537AYI6mKluv/ex22Bg5iDlpfQNiQw4h4aenhxRzA2re1\n7GF3sLluvfiV0ddDQt6VQJPyGer5HF8I7dCq1Lm/QHNwtN+x54Rs4YKDcR5q\nN3tpYpAXNtkD/Gcj5lXCKUrdgTeiQ7fT1kq4+bPlLMyjxIwYEAPOaM+gQuxP\nruCY/luz7Qy+Q1xoMNf6Cr2YI6eEiTM5eQbvPKViTT3XmZJ7uKOazJVfJSnh\njTdb\r\n=uaP8\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDVEMCXh0l1OlfvTQf3BqfTdIAgZ/SXv7PhU92yhHEqkAIhAPFUG4RjCn7ngiVWKe+eiQJYa/ZQU6Z9y1UzeccCNLxQ"}]},"_npmUser":{"name":"dataxquare","email":"info@dataxquare.com"},"directories":{},"maintainers":[{"name":"dataxquare","email":"info@dataxquare.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sse-pubsub_1.5.0_1641401381129_0.7880696242368166"},"_hasShrinkwrap":false},"1.5.1":{"name":"@dataxquare/sse-pubsub","version":"1.5.1","description":"Server-sent events with pub/sub interface","main":"index.js","scripts":{"test":"mocha test/tests.mjs --exit","start":"node demo/server.js","publish":"npm publish --access public"},"repository":{"type":"git","url":"git+ssh://git@github.com/triblondon/node-sse-pubsub.git"},"keywords":["sse","http","server","pubsub"],"author":{"name":"Andrew Betts","email":"andrew@trib.tv"},"license":"MIT","bugs":{"url":"https://github.com/triblondon/node-sse-pubsub/issues"},"homepage":"https://github.com/triblondon/node-sse-pubsub#readme","devDependencies":{"chai":"^4.3.4","express":"^4.17.1","mocha":"^9.1.3","node-fetch":"^3.0.0"},"gitHead":"ae13d9ffa6a61bd74b3de0b634b8c34dc972c1aa","_id":"@dataxquare/sse-pubsub@1.5.1","_nodeVersion":"14.16.1","_npmVersion":"7.13.0","dist":{"integrity":"sha512-WLkU17yj/FPi2MjwKneyUKp3VQtV5BD4cgkAPX2conE0MP78+CIIuvneIcdcWud1SsR2bFQH5983l2bcHPIIig==","shasum":"b8de7bcf453fcaae52e6ed6a3f97fd5f258ad190","tarball":"https://registry.npmjs.org/@dataxquare/sse-pubsub/-/sse-pubsub-1.5.1.tgz","fileCount":8,"unpackedSize":151293,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh1c2mCRA9TVsSAnZWagAAdG8QAJuCP7szfdBVFdfP73kO\nmczYCpyZJc1GWbYWXzKhY0Xjq1Mvx0+LashmNbv/hFm6v1hjBVgpM1SZ0vx4\nbwtswIrpZmGzXFzPJmqOnxa7iU9Dgc6dlxdQV2e3HxUrOdMFWs0cf2mr3w5n\nh6JKBQziyCSnqh2Ph5ZX5xY+3x6tmDOzw+HNYYhsPmmIOklzdgojMSPXu/Rc\nSsrPSirIOK14v0MUpjOgN6v82fS5hRL0jQmPhXGX53DsfJmL9iq49lnoMimK\nnb+J7JOy9oCQAjiV2kpvp8qLaDxIwtzE0Mg5ZoIMhKvpj9TxO+zv6s5HIW0y\nPh3RrkzbfRcWPtnzJUI0K1hOXtb0+XIV3fG7i3csJoeT2N81yECvPkIaR5Et\nBn2zL0JtFPPIe2QATR5EaaWwPsh9iHYYBHmh1dy5uBPdPcAzKOrudiIPrSrL\n57y65uR2L63M5r2HhjDBoXDNE3bVsg7U+qid8dfk8bZGlGdOd2p7U17EiY1d\nYb965mVCGfbHdSS/EeXO3Wx+9e+Pkzt3ejB6eG+avIv1SynEecoMvC8R7wVA\nPIyl3/O6sIz5nYd2KB2RZG0n8hBZHnY8p6zSVX8qsRRibQOuuAIK1k4h6GUx\n831IBesLDJqFzimDsmuBE/hpeUZBl9tsHMNdVL/FjMMwM12hZEDAISRtWBMB\n9FxW\r\n=yQgx\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDrTohf9bqb6yqkdKPyyrjR1t7hZlotY9KZHXxl/IBs+wIhAPlJpM+P7GDdAHsfPjFoSoddusIz8k8TZRNLvJrGNC7d"}]},"_npmUser":{"name":"dataxquare","email":"info@dataxquare.com"},"directories":{},"maintainers":[{"name":"dataxquare","email":"info@dataxquare.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sse-pubsub_1.5.1_1641401765942_0.6643868801126653"},"_hasShrinkwrap":false}},"time":{"created":"2022-01-05T16:49:41.079Z","1.5.0":"2022-01-05T16:49:41.278Z","modified":"2022-04-05T03:03:32.484Z","1.5.1":"2022-01-05T16:56:06.107Z"},"maintainers":[{"name":"dataxquare","email":"info@dataxquare.com"}],"description":"Server-sent events with pub/sub interface","homepage":"https://github.com/triblondon/node-sse-pubsub#readme","keywords":["sse","http","server","pubsub"],"repository":{"type":"git","url":"git+ssh://git@github.com/triblondon/node-sse-pubsub.git"},"author":{"name":"Andrew Betts","email":"andrew@trib.tv"},"bugs":{"url":"https://github.com/triblondon/node-sse-pubsub/issues"},"license":"MIT","readme":"# Server-sent events for NodeJS\n\nA simple NodeJS module to generate Server-Sent-Events streams with a publish/subscribe interface and simple integration with either Node's built in HTTP library or any framework that exposes it, eg. [ExpressJS](https://expressjs.com).\n\n## Usage\n\nInstall with `npm i sse-pubsub`, then, if you are using Express:\n\n```javascript\nconst express = require('express');\nconst SSEChannel = require('sse-pubsub');\n\nconst app = express();\nconst channel = new SSEChannel();\n\n// Say hello every second\nsetInterval(() => channel.publish(\"Hello everyone!\", 'myEvent'), 1000);\n\napp.get('/stream', (req, res) => channel.subscribe(req, res));\n\napp.listen(3000);\n```\n\nYou should now be able to visit `http://localhost:3000/stream` and see a sequence of 'Hello everyone!' events appearing once per second.\n\nTo subscribe to the stream from client-side JavaScript:\n\n```javascript\nconst es = new EventSource(\"/stream\");\nes.addEventListener('myEvent', ev => {\n\talert(ev.data);\n});\n```\n\nA more detailed demo lives in `/demo/` and can be run with `npm start` if the module is checked out for development (see [development](#development)).\n\n## API\n\n### `SSEChannel(options)` (constructor)\n\nCreates a new `SSEChannel`.  Available options are:\n\n* `pingInterval` (integer): Interval at which to send pings (no-op events).  Milliseconds, default is 3000, set to a falsy value to disable pings entirely.\n* `maxStreamDuration` (integer): Maximum amoiunt of time to allow a client to remain connected before closing the connection.  SSE streams are expected to disconnect regularly to avoid zombie clients and problems with misbehaving middleboxes and proxies.  Milliseconds, default is 30000.\n* `clientRetryInterval` (integer): Amount of time clients should wait before reconencting, if they become disconnected from the stream. Milliseconds, default is 1000.\n* `startId` (integer): ID to use for the first message on the channel.  Default is 1.\n* `historySize` (integer): Number of messages to keep in memory, allowing for clients to use the `Last-Event-ID` header to request events that occured before they joined the stream.  Default is 100.\n* `rewind` (integer): Number of messages to backtrack by default when serving a new subscriber that does not include a `Last-Event-ID` in their request. If request includes `Last-Event-ID`, the `rewind` option is ignored.\n\n```javascript\nconst channel = new SSEChannel({\n\tpingInterval: 10000,\n\tstartId: 1330\n});\n```\n\n### `subscribe(req, res, [events])`\n\nAttaches an inbound HTTP request to an SSE channel.  Usually used in conjuction with a framework like Express.  Returns a reference for the client.\n\n* `req`: A NodeJS `IncomingMessage` object or anything that inherits from it\n* `res`: A NodeJS `ServerResponse` object or anything that inherits from it\n* `events`: Optional, an array of event names to deliver to this subscriber.  If not provided, the subscriber will receive all events.\n\n```javascript\nconst channel = new SSEChannel();\napp.get('/stream', (req, res) => channel.subscribe(req, res));\n```\n\n### `publish(data, [eventName], [overwriteEventName])`\n\nPublishes a new event to all subscribers to the channel.\n\n* `data` (any): Message to send.  If a primitive value, will be sent as-is, otherwise will be passed through `JSON.stringify`.\n* `eventName` (string): Event name to assign to the event.\n* `overwriteEventName` (string): Event name to assign to the event, overwriting *eventName*.\n\nSince all events published to a channel will be sent to all subscribers to that channel, if you want to filter events that are of interest to only a subset of users, it makes sense to use multiple separate channel instances.  However, `eventName` is useful if you are sending events that are all relevant to all subscribers, but might need to be processed by different client-side handlers.  Event names can therefore be considered to be like method names of a method that you are invoking in the client-side code.\n\n`eventName` is also useful if you don't want to create multiple separate channel. In this case use eventName to filter and an optional `overwriteEventName` to set the same eventName for every subscription.\n\nReturns the ID of the new message.\n\n### `unsubscribe(clientRef)`\n\nDetaches an active HTTP connection from the channel.\n\n* `clientRef` (object): A reference to an object obtained by calling the `subscribe` method.\n\n### `close()`\n\nCloses all subscriber connections, deletes message history and stops ping timer.  If you intend to create temporary channels, ensure you close them before dereferencing, otherwise timers and HTTP clients may cause memory leaks.\n\n### `listClients()`\n\nReturns an object where the keys are the remote addresses of each connected client, and the values are the number of connections from that address.\n\n```javascript\nconsole.log(channel.listClients());\n// {\n//  '::ffff:127.0.0.1': 2\n// }\n```\n\n### `getSubscriberCount()`\n\nReturns the number of currently connected clients.\n\n## Development\n\nTo develop on the project, clone the repo and run `npm install`.  Then if you want to run the demo server:\n\n1. `npm start`\n2. Open `http://127.0.0.1:3101` in your browser\n\nTo run the tests, `npm test`.  The project uses Express for the demo server and Mocha and Chai for the tests, however none of these are needed in production. The tests use Node's raw http library rather than express to ensure there is not any accidental dependence on request or response properties added by Express.\n\n## Licence\n\nMIT.\n","readmeFilename":"README.md"}