Skip to content

Commit 509c0f0

Browse files
docs: add examples with TypeScript and ES6 modules
1 parent 0d2d69c commit 509c0f0

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
## Table of contents
77

88
- [How to use](#how-to-use)
9+
- [CommonJS](#commonjs)
10+
- [ES6 module](#es6-modules)
11+
- [TypeScript](#typescript)
912
- [Compatibility table](#compatibility-table)
1013
- [API](#api)
1114
- [adapter(uri[, opts])](#adapteruri-opts)
@@ -26,14 +29,41 @@
2629

2730
## How to use
2831

32+
### CommonJS
33+
2934
```js
3035
const io = require('socket.io')(3000);
3136
const redisAdapter = require('socket.io-redis');
3237
io.adapter(redisAdapter({ host: 'localhost', port: 6379 }));
3338
```
3439

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
3767
all broadcast and emit events to and from each other.
3868

3969
So any of the following commands:
@@ -185,9 +215,9 @@ a connection string.
185215
```js
186216
const redis = require('redis');
187217
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 }));
191221
```
192222

193223
## With ioredis client

0 commit comments

Comments
 (0)