diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc20be9..03dff2b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x, 14.x] + node-version: [12.x, 16.x] services: redis: diff --git a/Readme.md b/Readme.md index 46a5d4b..7834c8c 100644 --- a/Readme.md +++ b/Readme.md @@ -44,15 +44,37 @@ The current version is compatible with both: ## How to use +Installation: + +``` +npm i @socket.io/redis-emitter redis +``` + ### CommonJS -Installation: `npm i @socket.io/redis-emitter redis` +```js +const { Emitter } = require("@socket.io/redis-emitter"); +const { createClient } = require("redis"); // not included, needs to be explicitly installed + +const redisClient = createClient(); + +redisClient.connect().then(() => { + const io = new Emitter(redisClient); + + setInterval(() => { + io.emit("time", new Date); + }, 5000); +}) +``` + +With `redis@3`, calling `connect()` is not needed: ```js const { Emitter } = require("@socket.io/redis-emitter"); const { createClient } = require("redis"); // not included, needs to be explicitly installed const redisClient = createClient(); + const io = new Emitter(redisClient); setInterval(() => { @@ -62,18 +84,19 @@ setInterval(() => { ### TypeScript -Installation: `npm i @socket.io/redis-emitter redis @types/redis` - ```ts import { Emitter } from "@socket.io/redis-emitter"; import { createClient } from "redis"; const redisClient = createClient(); -const io = new Emitter(redisClient); -setInterval(() => { - io.emit("time", new Date); -}, 5000); +redisClient.connect().then(() => { + const io = new Emitter(redisClient); + + setInterval(() => { + io.emit("time", new Date); + }, 5000); +}); ``` With typed events: @@ -87,9 +110,12 @@ interface Events { } const redisClient = createClient(); -const io = new Emitter(redisClient); -io.emit("basicEmit", 1, "2", [3]); +redisClient.connect().then(() => { + const io = new Emitter(redisClient); + + io.emit("basicEmit", 1, "2", [3]); +}); ``` ## Emit cheatsheet diff --git a/package-lock.json b/package-lock.json index 9073151..72aa122 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,15 +14,14 @@ "socket.io-parser": "~4.0.4" }, "devDependencies": { - "@socket.io/redis-adapter": "^7.0.0", + "@socket.io/redis-adapter": "^7.1.0", "@types/mocha": "^8.2.1", "@types/node": "^14.14.35", - "@types/redis": "^2.8.28", "expect.js": "~0.3.1", "mocha": "^9.1.3", "nyc": "^15.1.0", "prettier": "^2.2.1", - "redis": "^3.0.2", + "redis": "^4.0.1", "socket.io": "^4.1.1", "socket.io-client": "^4.1.1", "ts-node": "^9.1.1", @@ -305,10 +304,52 @@ "node": ">=8" } }, + "node_modules/@node-redis/client": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@node-redis/client/-/client-1.0.1.tgz", + "integrity": "sha512-o0I4LdzJXP6QYxRnBPrQ7cIG5tF3SNM/PBnjC3mV6QkzIiGRElzWqSr9a9JCJdcyB1SIA80bhgGhpdTpCQ1Sdw==", + "dev": true, + "dependencies": { + "cluster-key-slot": "1.1.0", + "generic-pool": "3.8.2", + "redis-parser": "3.0.0", + "yallist": "4.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@node-redis/json": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@node-redis/json/-/json-1.0.1.tgz", + "integrity": "sha512-2EB96ZN0yUr4mgA9Odme48jX8eF5Ji0jrsRn4rLfEhME7L3rHLdKeUfxJKxbPOxadP6k8+6ViElxPZrKuV2nvQ==", + "dev": true, + "peerDependencies": { + "@node-redis/client": "^1.0.0" + } + }, + "node_modules/@node-redis/search": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@node-redis/search/-/search-1.0.1.tgz", + "integrity": "sha512-iA2Gw6gr0X6IfNSjTyme9W1tDlLkwQ1bPApo4s8aVwZ2Ju8Z4COVik0vT6BJPRin79f5xPZgnaec3DIoC2UpHA==", + "dev": true, + "peerDependencies": { + "@node-redis/client": "^1.0.0" + } + }, + "node_modules/@node-redis/time-series": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@node-redis/time-series/-/time-series-1.0.0.tgz", + "integrity": "sha512-QcaCIL/DlYJXedSfmjF+IRxKJbBUXBrjA5Gv0IiPlXXFFOkRnbPGKq6hmwBAAWyk1U03wyBHDFKVS3/9GnZV8g==", + "dev": true, + "peerDependencies": { + "@node-redis/client": "^1.0.0" + } + }, "node_modules/@socket.io/redis-adapter": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@socket.io/redis-adapter/-/redis-adapter-7.0.0.tgz", - "integrity": "sha512-ioqzSUH2XvrBKm70bphTeLwBSPUU0n+w/Qkz11ryKBn6IIT4GJxyVOpodVBGnmDpxhT31EImQTZRT1ZueSTiTQ==", + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/@socket.io/redis-adapter/-/redis-adapter-7.1.0.tgz", + "integrity": "sha512-vbsNJKUQgtVHcOqNL2ac8kSemTVNKHRzYPldqQJt0eFKvlAtAviuAMzBP0WmOp5OoRLQMjhVsVvgMzzMsVsK5g==", "dev": true, "dependencies": { "debug": "~4.3.1", @@ -355,15 +396,6 @@ "integrity": "sha512-Lt+wj8NVPx0zUmUwumiVXapmaLUcAk3yPuHCFVXras9k5VT9TdhJqKqGVUQCD60OTMCl0qxJ57OiTL0Mic3Iag==", "dev": true }, - "node_modules/@types/redis": { - "version": "2.8.28", - "resolved": "https://registry.npmjs.org/@types/redis/-/redis-2.8.28.tgz", - "integrity": "sha512-8l2gr2OQ969ypa7hFOeKqtFoY70XkHxISV0pAwmQ2nm6CSPb1brmTmqJCGGrekCo+pAZyWlNXr+Kvo6L/1wijA==", - "dev": true, - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@ungap/promise-all-settled": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", @@ -684,6 +716,15 @@ "wrap-ansi": "^6.2.0" } }, + "node_modules/cluster-key-slot": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz", + "integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/color-convert": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", @@ -810,15 +851,6 @@ "node": ">=8" } }, - "node_modules/denque": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz", - "integrity": "sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ==", - "dev": true, - "engines": { - "node": ">=0.10" - } - }, "node_modules/diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", @@ -1028,6 +1060,15 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, + "node_modules/generic-pool": { + "version": "3.8.2", + "resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.8.2.tgz", + "integrity": "sha512-nGToKy6p3PAbYQ7p1UlWl6vSPwfwU6TMSWK7TTu+WUY4ZjyZQGniGGt2oNVvyNSpyZYSB43zMXVLcBm08MTMkg==", + "dev": true, + "engines": { + "node": ">= 4" + } + }, "node_modules/gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", @@ -2151,30 +2192,20 @@ } }, "node_modules/redis": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz", - "integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.0.1.tgz", + "integrity": "sha512-qfcq1oz2ci7pNdCfTLLEuKhS8jZ17dFiT1exogOr+jd3EVP/h9qpy7K+VajB4BXA0k8q68KFqR6HrliKV6jt1Q==", "dev": true, "dependencies": { - "denque": "^1.5.0", - "redis-commands": "^1.7.0", - "redis-errors": "^1.2.0", - "redis-parser": "^3.0.0" + "@node-redis/client": "^1.0.1", + "@node-redis/json": "^1.0.1", + "@node-redis/search": "^1.0.1", + "@node-redis/time-series": "^1.0.0" }, "engines": { - "node": ">=10" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/node-redis" + "npm": ">=7" } }, - "node_modules/redis-commands": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz", - "integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==", - "dev": true - }, "node_modules/redis-errors": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", @@ -2725,6 +2756,12 @@ "integrity": "sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==", "dev": true }, + "node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + }, "node_modules/yargs": { "version": "15.4.1", "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", @@ -3082,10 +3119,43 @@ "integrity": "sha512-ZXRY4jNvVgSVQ8DL3LTcakaAtXwTVUxE81hslsyD2AtoXW/wVob10HkOJ1X/pAlcI7D+2YoZKg5do8G/w6RYgA==", "dev": true }, + "@node-redis/client": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@node-redis/client/-/client-1.0.1.tgz", + "integrity": "sha512-o0I4LdzJXP6QYxRnBPrQ7cIG5tF3SNM/PBnjC3mV6QkzIiGRElzWqSr9a9JCJdcyB1SIA80bhgGhpdTpCQ1Sdw==", + "dev": true, + "requires": { + "cluster-key-slot": "1.1.0", + "generic-pool": "3.8.2", + "redis-parser": "3.0.0", + "yallist": "4.0.0" + } + }, + "@node-redis/json": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@node-redis/json/-/json-1.0.1.tgz", + "integrity": "sha512-2EB96ZN0yUr4mgA9Odme48jX8eF5Ji0jrsRn4rLfEhME7L3rHLdKeUfxJKxbPOxadP6k8+6ViElxPZrKuV2nvQ==", + "dev": true, + "requires": {} + }, + "@node-redis/search": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@node-redis/search/-/search-1.0.1.tgz", + "integrity": "sha512-iA2Gw6gr0X6IfNSjTyme9W1tDlLkwQ1bPApo4s8aVwZ2Ju8Z4COVik0vT6BJPRin79f5xPZgnaec3DIoC2UpHA==", + "dev": true, + "requires": {} + }, + "@node-redis/time-series": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@node-redis/time-series/-/time-series-1.0.0.tgz", + "integrity": "sha512-QcaCIL/DlYJXedSfmjF+IRxKJbBUXBrjA5Gv0IiPlXXFFOkRnbPGKq6hmwBAAWyk1U03wyBHDFKVS3/9GnZV8g==", + "dev": true, + "requires": {} + }, "@socket.io/redis-adapter": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@socket.io/redis-adapter/-/redis-adapter-7.0.0.tgz", - "integrity": "sha512-ioqzSUH2XvrBKm70bphTeLwBSPUU0n+w/Qkz11ryKBn6IIT4GJxyVOpodVBGnmDpxhT31EImQTZRT1ZueSTiTQ==", + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/@socket.io/redis-adapter/-/redis-adapter-7.1.0.tgz", + "integrity": "sha512-vbsNJKUQgtVHcOqNL2ac8kSemTVNKHRzYPldqQJt0eFKvlAtAviuAMzBP0WmOp5OoRLQMjhVsVvgMzzMsVsK5g==", "dev": true, "requires": { "debug": "~4.3.1", @@ -3131,15 +3201,6 @@ "integrity": "sha512-Lt+wj8NVPx0zUmUwumiVXapmaLUcAk3yPuHCFVXras9k5VT9TdhJqKqGVUQCD60OTMCl0qxJ57OiTL0Mic3Iag==", "dev": true }, - "@types/redis": { - "version": "2.8.28", - "resolved": "https://registry.npmjs.org/@types/redis/-/redis-2.8.28.tgz", - "integrity": "sha512-8l2gr2OQ969ypa7hFOeKqtFoY70XkHxISV0pAwmQ2nm6CSPb1brmTmqJCGGrekCo+pAZyWlNXr+Kvo6L/1wijA==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@ungap/promise-all-settled": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", @@ -3394,6 +3455,12 @@ "wrap-ansi": "^6.2.0" } }, + "cluster-key-slot": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz", + "integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==", + "dev": true + }, "color-convert": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", @@ -3497,12 +3564,6 @@ "strip-bom": "^4.0.0" } }, - "denque": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz", - "integrity": "sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ==", - "dev": true - }, "diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", @@ -3651,6 +3712,12 @@ "dev": true, "optional": true }, + "generic-pool": { + "version": "3.8.2", + "resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.8.2.tgz", + "integrity": "sha512-nGToKy6p3PAbYQ7p1UlWl6vSPwfwU6TMSWK7TTu+WUY4ZjyZQGniGGt2oNVvyNSpyZYSB43zMXVLcBm08MTMkg==", + "dev": true + }, "gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", @@ -4485,23 +4552,17 @@ } }, "redis": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz", - "integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.0.1.tgz", + "integrity": "sha512-qfcq1oz2ci7pNdCfTLLEuKhS8jZ17dFiT1exogOr+jd3EVP/h9qpy7K+VajB4BXA0k8q68KFqR6HrliKV6jt1Q==", "dev": true, "requires": { - "denque": "^1.5.0", - "redis-commands": "^1.7.0", - "redis-errors": "^1.2.0", - "redis-parser": "^3.0.0" + "@node-redis/client": "^1.0.1", + "@node-redis/json": "^1.0.1", + "@node-redis/search": "^1.0.1", + "@node-redis/time-series": "^1.0.0" } }, - "redis-commands": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz", - "integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==", - "dev": true - }, "redis-errors": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", @@ -4918,6 +4979,12 @@ "integrity": "sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==", "dev": true }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + }, "yargs": { "version": "15.4.1", "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", diff --git a/package.json b/package.json index 292e838..b133b41 100644 --- a/package.json +++ b/package.json @@ -24,15 +24,14 @@ "socket.io-parser": "~4.0.4" }, "devDependencies": { - "@socket.io/redis-adapter": "^7.0.0", + "@socket.io/redis-adapter": "^7.1.0", "@types/mocha": "^8.2.1", "@types/node": "^14.14.35", - "@types/redis": "^2.8.28", "expect.js": "~0.3.1", "mocha": "^9.1.3", "nyc": "^15.1.0", "prettier": "^2.2.1", - "redis": "^3.0.2", + "redis": "^4.0.1", "socket.io": "^4.1.1", "socket.io-client": "^4.1.1", "ts-node": "^9.1.1", diff --git a/test/index.ts b/test/index.ts index 23a2325..5cda7c5 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,5 +1,5 @@ import expect = require("expect.js"); -import { createClient, RedisClient } from "redis"; +import { createClient, RedisClientType } from "redis"; import { Server, Socket } from "socket.io"; import { io as ioc, Socket as ClientSocket } from "socket.io-client"; import { createAdapter } from "@socket.io/redis-adapter"; @@ -31,18 +31,20 @@ const createPartialDone = ( describe("emitter", () => { let port: number, io: Server, - pubClient: RedisClient, - subClient: RedisClient, + pubClient: RedisClientType, + subClient: RedisClientType, serverSockets: Socket[], clientSockets: ClientSocket[], emitter: Emitter; - beforeEach((done) => { + beforeEach(async () => { const httpServer = createServer(); pubClient = createClient(); subClient = createClient(); + await Promise.all([pubClient.connect(), subClient.connect()]); + io = new Server(httpServer, { adapter: createAdapter(pubClient, subClient), }); @@ -56,14 +58,17 @@ describe("emitter", () => { }); serverSockets = []; - io.on("connection", (socket) => { - serverSockets.push(socket); - if (serverSockets.length === SOCKETS_COUNT) { - setTimeout(done, 100); - } - }); emitter = new Emitter(pubClient); + + return new Promise((resolve) => { + io.on("connection", (socket) => { + serverSockets.push(socket); + if (serverSockets.length === SOCKETS_COUNT) { + setTimeout(resolve, 100); + } + }); + }); }); afterEach(() => {