|
6 | 6 | ## Table of contents |
7 | 7 |
|
8 | 8 | - [How to use](#how-to-use) |
| 9 | + - [CommonJS](#commonjs) |
| 10 | + - [ES6 module](#es6-modules) |
| 11 | + - [TypeScript](#typescript) |
9 | 12 | - [Compatibility table](#compatibility-table) |
10 | 13 | - [API](#api) |
11 | 14 | - [adapter(uri[, opts])](#adapteruri-opts) |
|
26 | 29 |
|
27 | 30 | ## How to use |
28 | 31 |
|
| 32 | +### CommonJS |
| 33 | + |
29 | 34 | ```js |
30 | 35 | const io = require('socket.io')(3000); |
31 | 36 | const redisAdapter = require('socket.io-redis'); |
32 | 37 | io.adapter(redisAdapter({ host: 'localhost', port: 6379 })); |
33 | 38 | ``` |
34 | 39 |
|
35 | | -By running socket.io with the `socket.io-redis` adapter you can run |
36 | | -multiple socket.io instances in different processes or servers that can |
| 40 | +### ES6 modules |
| 41 | + |
| 42 | +```js |
| 43 | +import { Server } from 'socket.io'; |
| 44 | +import redisAdapter from 'socket.io-redis'; |
| 45 | + |
| 46 | +const io = new Server(3000); |
| 47 | +io.adapter(redisAdapter({ host: 'localhost', port: 6379 })); |
| 48 | +``` |
| 49 | + |
| 50 | +### TypeScript |
| 51 | + |
| 52 | +```ts |
| 53 | +// npm i -D @types/redis |
| 54 | +import { Server } from 'socket.io'; |
| 55 | +import { createAdapter } from 'socket.io-redis'; |
| 56 | +import { RedisClient } from 'redis'; |
| 57 | + |
| 58 | +const io = new Server(8080); |
| 59 | +const pubClient = new RedisClient({ host: 'localhost', port: 6379 }); |
| 60 | +const subClient = pubClient.duplicate(); |
| 61 | + |
| 62 | +io.adapter(createAdapter({ pubClient, subClient })); |
| 63 | +``` |
| 64 | + |
| 65 | +By running Socket.IO with the `socket.io-redis` adapter you can run |
| 66 | +multiple Socket.IO instances in different processes or servers that can |
37 | 67 | all broadcast and emit events to and from each other. |
38 | 68 |
|
39 | 69 | So any of the following commands: |
@@ -185,9 +215,9 @@ a connection string. |
185 | 215 | ```js |
186 | 216 | const redis = require('redis'); |
187 | 217 | const redisAdapter = require('socket.io-redis'); |
188 | | -const pub = redis.createClient(port, host, { auth_pass: "pwd" }); |
189 | | -const sub = redis.createClient(port, host, { auth_pass: "pwd" }); |
190 | | -io.adapter(redisAdapter({ pubClient: pub, subClient: sub })); |
| 218 | +const pubClient = redis.createClient(port, host, { auth_pass: "pwd" }); |
| 219 | +const subClient = pubClient.duplicate(); |
| 220 | +io.adapter(redisAdapter({ pubClient, subClient })); |
191 | 221 | ``` |
192 | 222 |
|
193 | 223 | ## With ioredis client |
|
0 commit comments