From d55d6e5fc986b9ee7a6b866a9694d8f0b005eb8d Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 4 Jan 2022 08:39:18 +0100 Subject: [PATCH] docs: fix example with redis v4 It seems the clients must be connected before creating the adapter. Related: https://github.com/socketio/socket.io-redis-adapter/issues/436 --- README.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 8c381b1..c1c1e5c 100644 --- a/README.md +++ b/README.md @@ -48,23 +48,18 @@ const io = new Server(); const pubClient = createClient({ host: 'localhost', port: 6379 }); const subClient = pubClient.duplicate(); -io.adapter(createAdapter(pubClient, subClient)); - -// redis@3 -io.listen(3000); - -// redis@4 Promise.all([pubClient.connect(), subClient.connect()]).then(() => { + io.adapter(createAdapter(pubClient, subClient)); io.listen(3000); }); ``` -### ES6 modules +With `redis@3`, calling `connect()` on the Redis clients is not needed: ```js -import { Server } from 'socket.io'; -import { createClient } from 'redis'; -import { createAdapter } from '@socket.io/redis-adapter'; +const { Server } = require('socket.io'); +const { createClient } = require('redis'); +const { createAdapter } = require('@socket.io/redis-adapter'); const io = new Server(); const pubClient = createClient({ host: 'localhost', port: 6379 }); @@ -72,11 +67,22 @@ const subClient = pubClient.duplicate(); io.adapter(createAdapter(pubClient, subClient)); -// redis@3 io.listen(3000); +``` + +### ES6 modules + +```js +import { Server } from 'socket.io'; +import { createClient } from 'redis'; +import { createAdapter } from '@socket.io/redis-adapter'; + +const io = new Server(); +const pubClient = createClient({ host: 'localhost', port: 6379 }); +const subClient = pubClient.duplicate(); -// redis@4 Promise.all([pubClient.connect(), subClient.connect()]).then(() => { + io.adapter(createAdapter(pubClient, subClient)); io.listen(3000); }); ``` @@ -84,22 +90,16 @@ Promise.all([pubClient.connect(), subClient.connect()]).then(() => { ### TypeScript ```ts -// npm i -D redis @types/redis import { Server } from 'socket.io'; +import { createClient } from 'redis'; import { createAdapter } from '@socket.io/redis-adapter'; -import { RedisClient } from 'redis'; const io = new Server(); const pubClient = createClient({ host: 'localhost', port: 6379 }); const subClient = pubClient.duplicate(); -io.adapter(createAdapter(pubClient, subClient)); - -// redis@3 -io.listen(3000); - -// redis@4 Promise.all([pubClient.connect(), subClient.connect()]).then(() => { + io.adapter(createAdapter(pubClient, subClient)); io.listen(3000); }); ```