Skip to content

Commit 39eaa0e

Browse files
feat: implement the Redis adapter
We had to make a few changes to the redis module (in the vendor/ directory), in order to implement the feature. We will remove it once the changes are merged upstream. See also: denodrivers/redis#335 This implementation is compatible with the Node.js one (same format for the Redis channels and the packets). See also: https://github.com/socketio/socket.io-redis-adapter
1 parent 96a227b commit 39eaa0e

27 files changed

+2995
-24
lines changed

.github/workflows/ci.yml

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ jobs:
1515
deno-version:
1616
- 1.25.2
1717

18+
services:
19+
redis:
20+
image: redis
21+
options: >-
22+
--health-cmd "redis-cli ping"
23+
--health-interval 10s
24+
--health-timeout 5s
25+
--health-retries 5
26+
ports:
27+
- 6379:6379
28+
1829
steps:
1930
- name: Checkout repository
2031
uses: actions/checkout@v2

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Table of content:
1717
- [`editHandshakeHeaders`](#edithandshakeheaders)
1818
- [`editResponseHeaders`](#editresponseheaders)
1919
- [Logs](#logs)
20+
- [Adapters](#adapters)
21+
- [Redis adapter](#redis-adapter)
2022

2123
## Usage
2224

@@ -231,6 +233,41 @@ await log.setup({
231233
});
232234
```
233235

236+
## Adapters
237+
238+
Custom adapters can be used to broadcast packets between several Socket.IO
239+
servers.
240+
241+
### Redis adapter
242+
243+
Documentation: https://socket.io/docs/v4/redis-adapter/
244+
245+
```js
246+
import { serve } from "https://deno.land/std/http/server.ts";
247+
import {
248+
createRedisAdapter,
249+
createRedisClient,
250+
Server,
251+
} from "https://deno.land/x/socket_io/mod.ts";
252+
253+
const [pubClient, subClient] = await Promise.all([
254+
createRedisClient({
255+
hostname: "localhost",
256+
}),
257+
createRedisClient({
258+
hostname: "localhost",
259+
}),
260+
]);
261+
262+
const io = new Server({
263+
adapter: createRedisAdapter(pubClient, subClient),
264+
});
265+
266+
await serve(io.handler(), {
267+
port: 3000,
268+
});
269+
```
270+
234271
## License
235272

236273
[ISC](/LICENSE)

docker-compose.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "3.0"
2+
services:
3+
redis:
4+
image: redis:5
5+
ports:
6+
- "6379:6379"

mod.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
export { Server, type ServerOptions } from "./packages/socket.io/mod.ts";
1+
export {
2+
Adapter,
3+
type BroadcastOptions,
4+
type Namespace,
5+
Server,
6+
type ServerOptions,
7+
type Socket,
8+
} from "./packages/socket.io/mod.ts";
9+
10+
/**
11+
* The Redis adapter, to broadcast packets between several Socket.IO servers
12+
*
13+
* Documentation: https://socket.io/docs/v4/redis-adapter/
14+
*
15+
* Usage:
16+
*
17+
* ```
18+
* import { serve } from "https://deno.land/std/http/server.ts";
19+
* import { Server, createRedisAdapter, createRedisClient } from "https://deno.land/x/socket_io/mod.ts";
20+
*
21+
* const [pubClient, subClient] = await Promise.all([
22+
* createRedisClient({
23+
* hostname: "localhost",
24+
* }),
25+
* createRedisClient({
26+
* hostname: "localhost",
27+
* })
28+
* ]);
29+
*
30+
* const io = new Server({
31+
* adapter: createRedisAdapter(pubClient, subClient)
32+
* });
33+
*
34+
* await serve(io.handler(), {
35+
* port: 3000
36+
* });
37+
* ```
38+
*/
39+
export {
40+
createAdapter as createRedisAdapter,
41+
type RedisAdapterOptions,
42+
} from "./packages/socket.io-redis-adapter/mod.ts";
43+
44+
/**
45+
* Temporary export to provide a workaround for https://github.com/denodrivers/redis/issues/335
46+
*/
47+
export { connect as createRedisClient } from "./vendor/deno.land/x/[email protected]/mod.ts";

0 commit comments

Comments
 (0)