{"_id":"@blueberry6401/socker.io-redis-adapter","name":"@blueberry6401/socker.io-redis-adapter","dist-tags":{"latest":"8.2.1"},"versions":{"8.2.1":{"name":"@blueberry6401/socker.io-redis-adapter","version":"8.2.1","description":"The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers","license":"MIT","repository":{"type":"git","url":"git+ssh://git@github.com/socketio/socket.io-redis-adapter.git"},"main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"test":"npm run format:check && tsc && nyc mocha --bail --require ts-node/register test/test-runner.ts","format:check":"prettier --parser typescript --check 'lib/**/*.ts' 'test/**/*.ts'","format:fix":"prettier --parser typescript --write 'lib/**/*.ts' 'test/**/*.ts'","prepack":"tsc"},"dependencies":{"debug":"~4.3.1","notepack.io":"~3.0.1","uid2":"1.0.0"},"peerDependencies":{"socket.io-adapter":"^2.4.0"},"devDependencies":{"@types/expect.js":"^0.3.29","@types/mocha":"^8.2.1","@types/node":"^14.14.7","expect.js":"0.3.1","ioredis":"^5.3.2","mocha":"^10.1.0","nyc":"^15.1.0","prettier":"^2.8.7","redis":"^4.6.6","redis-v3":"npm:redis@^3.1.2","socket.io":"^4.6.1","socket.io-client":"^4.1.1","ts-node":"^10.9.1","typescript":"^4.9.5"},"engines":{"node":">=10.0.0"},"gitHead":"6785ac29053c61dd482e2e5a3fb4c1291eaf72e1","bugs":{"url":"https://github.com/socketio/socket.io-redis-adapter/issues"},"homepage":"https://github.com/socketio/socket.io-redis-adapter#readme","_id":"@blueberry6401/socker.io-redis-adapter@8.2.1","_nodeVersion":"16.19.1","_npmVersion":"8.19.3","dist":{"integrity":"sha512-ek+wkbplT8An9HNFrOW2TOu5huQbhECJKbNv+w4+jmp4cdqejMJXUiuxBql+TBnInrzNIp7/Wrb4QcwbRQoCOg==","shasum":"98ea872603216469392012dfcfc3cba436b296f3","tarball":"https://registry.npmjs.org/@blueberry6401/socker.io-redis-adapter/-/socker.io-redis-adapter-8.2.1.tgz","fileCount":11,"unpackedSize":79705,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICpr8srIE/uR1svWG5d8NVbx3fozAJzJiSrRwcArEbgcAiA/I5keYdps39j0SDCLNeImZIVblAjL9XUhjVUYKdoHTg=="}]},"_npmUser":{"name":"blueberry6401","email":"blueberry6401@gmail.com"},"directories":{},"maintainers":[{"name":"blueberry6401","email":"blueberry6401@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/socker.io-redis-adapter_8.2.1_1688371430653_0.5781541329521589"},"_hasShrinkwrap":false}},"time":{"created":"2023-07-03T08:03:50.592Z","8.2.1":"2023-07-03T08:03:50.876Z","modified":"2023-07-03T08:03:51.057Z"},"maintainers":[{"name":"blueberry6401","email":"blueberry6401@gmail.com"}],"description":"The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers","homepage":"https://github.com/socketio/socket.io-redis-adapter#readme","repository":{"type":"git","url":"git+ssh://git@github.com/socketio/socket.io-redis-adapter.git"},"bugs":{"url":"https://github.com/socketio/socket.io-redis-adapter/issues"},"license":"MIT","readme":"# socket.io-redis\n\n[![Build Status](https://github.com/socketio/socket.io-redis/workflows/CI/badge.svg?branch=main)](https://github.com/socketio/socket.io-redis/actions)\n[![npm version](https://badge.fury.io/js/%40socket.io%2Fredis-adapter.svg)](https://badge.fury.io/js/%40socket.io%2Fredis-adapter)\n\n## Table of contents\n\n- [How to use](#how-to-use)\n  - [CommonJS](#commonjs)\n  - [ES6 module](#es6-modules)\n  - [TypeScript](#typescript)\n  - [Sharded Redis Pub/Sub](#sharded-redis-pubsub)\n- [Compatibility table](#compatibility-table)\n- [How does it work under the hood?](#how-does-it-work-under-the-hood)\n- [API](#api)\n  - [adapter(pubClient, subClient[, opts])](#adapterpubclient-subclient-opts)\n  - [RedisAdapter](#redisadapter)\n    - [RedisAdapter#allRooms()](#redisadapterallrooms)\n- [With ioredis client](#with-ioredis-client)\n  - [Cluster example](#cluster-example)\n  - [Sentinel Example](#sentinel-example)\n- [Protocol](#protocol)\n- [Migrating from `socket.io-redis`](#migrating-from-socketio-redis)\n- [License](#license)\n\n## How to use\n\nInstallation:\n\n```\n$ npm install @socket.io/redis-adapter redis\n```\n\n### CommonJS\n\n```js\nconst { Server } = require('socket.io');\nconst { createClient } = require('redis');\nconst { createAdapter } = require('@socket.io/redis-adapter');\n\nconst io = new Server();\nconst pubClient = createClient({ host: 'localhost', port: 6379 });\nconst subClient = pubClient.duplicate();\n\nPromise.all([pubClient.connect(), subClient.connect()]).then(() => {\n  io.adapter(createAdapter(pubClient, subClient));\n  io.listen(3000);\n});\n```\n\nWith `redis@3`, calling `connect()` on the Redis clients is not needed:\n\n```js\nconst { Server } = require('socket.io');\nconst { createClient } = require('redis');\nconst { createAdapter } = require('@socket.io/redis-adapter');\n\nconst io = new Server();\nconst pubClient = createClient({ host: 'localhost', port: 6379 });\nconst subClient = pubClient.duplicate();\n\nio.adapter(createAdapter(pubClient, subClient));\n\nio.listen(3000);\n```\n\n### ES6 modules\n\n```js\nimport { Server } from 'socket.io';\nimport { createClient } from 'redis';\nimport { createAdapter } from '@socket.io/redis-adapter';\n\nconst io = new Server();\nconst pubClient = createClient({ host: 'localhost', port: 6379 });\nconst subClient = pubClient.duplicate();\n\nPromise.all([pubClient.connect(), subClient.connect()]).then(() => {\n  io.adapter(createAdapter(pubClient, subClient));\n  io.listen(3000);\n});\n```\n\n### TypeScript\n\n```ts\nimport { Server } from 'socket.io';\nimport { createClient } from 'redis';\nimport { createAdapter } from '@socket.io/redis-adapter';\n\nconst io = new Server();\nconst pubClient = createClient({ host: 'localhost', port: 6379 });\nconst subClient = pubClient.duplicate();\n\nPromise.all([pubClient.connect(), subClient.connect()]).then(() => {\n  io.adapter(createAdapter(pubClient, subClient));\n  io.listen(3000);\n});\n```\n\nBy running Socket.IO with the `@socket.io/redis-adapter` adapter you can run\nmultiple Socket.IO instances in different processes or servers that can\nall broadcast and emit events to and from each other.\n\nSo any of the following commands:\n\n```js\nio.emit('hello', 'to all clients');\nio.to('room42').emit('hello', \"to all clients in 'room42' room\");\n\nio.on('connection', (socket) => {\n  socket.broadcast.emit('hello', 'to all clients except sender');\n  socket.to('room42').emit('hello', \"to all clients in 'room42' room except sender\");\n});\n```\n\nwill properly be broadcast to the clients through the Redis [Pub/Sub mechanism](https://redis.io/topics/pubsub).\n\nIf you need to emit events to socket.io instances from a non-socket.io\nprocess, you should use [socket.io-emitter](https://github.com/socketio/socket.io-emitter).\n\n### Sharded Redis Pub/Sub\n\nSharded Pub/Sub was introduced in Redis 7.0 in order to help scaling the usage of Pub/Sub in cluster mode.\n\nReference: https://redis.io/docs/manual/pubsub/#sharded-pubsub\n\nA dedicated adapter can be created with the `createShardedAdapter()` method:\n\n```js\nimport { Server } from 'socket.io';\nimport { createClient } from 'redis';\nimport { createShardedAdapter } from '@socket.io/redis-adapter';\n\nconst pubClient = createClient({ host: 'localhost', port: 6379 });\nconst subClient = pubClient.duplicate();\n\nawait Promise.all([\n  pubClient.connect(),\n  subClient.connect()\n]);\n\nconst io = new Server({\n  adapter: createShardedAdapter(pubClient, subClient)\n});\n\nio.listen(3000);\n```\n\nMinimum requirements:\n\n- Redis 7.0\n- [`redis@4.6.0`](https://github.com/redis/node-redis/commit/3b1bad229674b421b2bc6424155b20d4d3e45bd1)\n\n\n## Compatibility table\n\n| Redis Adapter version | Socket.IO server version |\n|-----------------------| ------------------------ |\n| 4.x                   | 1.x                      |\n| 5.x                   | 2.x                      |\n| 6.0.x                 | 3.x                      |\n| 6.1.x and above       | 4.x                      |\n\n## How does it work under the hood?\n\nThis adapter extends the [in-memory adapter](https://github.com/socketio/socket.io-adapter/) that is included by default with the Socket.IO server.\n\nThe in-memory adapter stores the relationships between Sockets and Rooms in two Maps.\n\nWhen you run `socket.join(\"room21\")`, here's what happens:\n\n```\nconsole.log(adapter.rooms); // Map { \"room21\" => Set { \"mdpk4kxF5CmhwfCdAHD8\" } }\nconsole.log(adapter.sids); // Map { \"mdpk4kxF5CmhwfCdAHD8\" => Set { \"mdpk4kxF5CmhwfCdAHD8\", \"room21\" } }\n// \"mdpk4kxF5CmhwfCdAHD8\" being the ID of the given socket\n```\n\nThose two Maps are then used when broadcasting:\n\n- a broadcast to all sockets (`io.emit()`) loops through the `sids` Map, and send the packet to all sockets\n- a broadcast to a given room (`io.to(\"room21\").emit()`) loops through the Set in the `rooms` Map, and sends the packet to all matching sockets\n\nThe Redis adapter extends the broadcast function of the in-memory adapter: the packet is also [published](https://redis.io/topics/pubsub) to a Redis channel (see [below](#protocol) for the format of the channel name).\n\nEach Socket.IO server receives this packet and broadcasts it to its own list of connected sockets.\n\nTo check what's happening on your Redis instance:\n\n```\n$ redis-cli\n127.0.0.1:6379> PSUBSCRIBE *\nReading messages... (press Ctrl-C to quit)\n1) \"psubscribe\"\n2) \"*\"\n3) (integer) 1\n\n1) \"pmessage\"\n2) \"*\"\n3) \"socket.io#/#\" (a broadcast to all sockets or to a list of rooms)\n4) <the packet content>\n\n1) \"pmessage\"\n2) \"*\"\n3) \"socket.io#/#room21#\" (a broadcast to a single room)\n4) <the packet content>\n```\n\nNote: **no data** is stored in Redis itself\n\nThere are 3 Redis subscriptions per namespace:\n\n- main channel: `<prefix>#<namespace>#*` (glob)\n- request channel: `<prefix>-request#<namespace>#`\n- response channel: `<prefix>-response#<namespace>#`\n\nThe request and response channels are used in the additional methods exposed by the Redis adapter, like [RedisAdapter#allRooms()](#redisadapterallrooms).\n\n\n## API\n\n### adapter(pubClient, subClient[, opts])\n\nThe following options are allowed:\n\n- `key`: the name of the key to pub/sub events on as prefix (`socket.io`)\n- `requestsTimeout`: optional, after this timeout the adapter will stop waiting from responses to request (`5000ms`)\n- `parser`: optional, parser to use for encoding and decoding messages passed through Redis ([`notepack.io`](https://www.npmjs.com/package/notepack.io))\n\n### RedisAdapter\n\nThe redis adapter instances expose the following properties\nthat a regular `Adapter` does not\n\n- `uid`\n- `prefix`\n- `pubClient`\n- `subClient`\n- `requestsTimeout`\n- `parser`\n\n### RedisAdapter#allRooms()\n\nReturns the list of all rooms.\n\n```js\nconst rooms = await io.of('/').adapter.allRooms();\nconsole.log(rooms); // a Set containing all rooms (across every node)\n```\n\n## With ioredis client\n\n### Cluster example\n\n```js\nconst io = require('socket.io')(3000);\nconst redisAdapter = require('@socket.io/redis-adapter');\nconst Redis = require('ioredis');\n\nconst startupNodes = [\n  {\n    port: 6380,\n    host: '127.0.0.1'\n  },\n  {\n    port: 6381,\n    host: '127.0.0.1'\n  }\n];\n\nconst pubClient = new Redis.Cluster(startupNodes);\nconst subClient = pubClient.duplicate();\n\nio.adapter(redisAdapter(pubClient, subClient));\n```\n\n### Sentinel Example\n\n```js\nconst io = require('socket.io')(3000);\nconst redisAdapter = require('@socket.io/redis-adapter');\nconst Redis = require('ioredis');\n\nconst options = {\n  sentinels: [\n    { host: 'somehost1', port: 26379 },\n    { host: 'somehost2', port: 26379 }\n  ],\n  name: 'master01'\n};\n\nconst pubClient = new Redis(options);\nconst subClient = pubClient.duplicate();\n\nio.adapter(redisAdapter(pubClient, subClient));\n```\n\n## Protocol\n\nThe `@socket.io/redis-adapter` adapter broadcasts and receives messages on particularly named Redis channels. For global broadcasts the channel name is:\n```\nprefix + '#' + namespace + '#'\n```\n\nIn broadcasting to a single room the channel name is:\n```\nprefix + '#' + namespace + '#' + room + '#'\n```\n\n\n- `prefix`: The base channel name. Default value is `socket.io`. Changed by setting `opts.key` in `adapter(opts)` constructor\n- `namespace`: See https://github.com/socketio/socket.io#namespace.\n- `room` : Used if targeting a specific room.\n\nA number of other libraries adopt this protocol including:\n\n- [socket.io-redis-emitter](https://github.com/socketio/socket.io-redis-emitter)\n- [socket.io-python-emitter](https://github.com/GameXG/socket.io-python-emitter)\n- [socket.io-emitter-go](https://github.com/stackcats/socket.io-emitter-go)\n\n## Migrating from `socket.io-redis`\n\nThe package was renamed from `socket.io-redis` to `@socket.io/redis-adapter` in [v7](https://github.com/socketio/socket.io-redis-adapter/releases/tag/7.0.0), in order to match the name of the Redis emitter (`@socket.io/redis-emitter`).\n\nTo migrate to the new package, you'll need to make sure to provide your own Redis clients, as the package will no longer create Redis clients on behalf of the user.\n\nBefore:\n\n```js\nconst redisAdapter = require(\"socket.io-redis\");\n\nio.adapter(redisAdapter({ host: \"localhost\", port: 6379 }));\n```\n\nAfter:\n\n```js\nconst { createClient } = require(\"redis\");\nconst { createAdapter } = require(\"@socket.io/redis-adapter\");\n\nconst pubClient = createClient({ host: \"localhost\", port: 6379 });\nconst subClient = pubClient.duplicate();\n\nio.adapter(createAdapter(pubClient, subClient));\n```\n\nPlease note that the communication protocol between the Socket.IO servers has not been updated, so you can have some servers with `socket.io-redis` and some others with `@socket.io/redis-adapter` at the same time.\n\n## License\n\nMIT\n","readmeFilename":"README.md"}