Skip to content

Commit

Permalink
feat: implement the Redis adapter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Oct 4, 2022
1 parent 96a227b commit 39eaa0e
Show file tree
Hide file tree
Showing 27 changed files with 2,995 additions and 24 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ jobs:
deno-version:
- 1.25.2

services:
redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379

steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Table of content:
- [`editHandshakeHeaders`](#edithandshakeheaders)
- [`editResponseHeaders`](#editresponseheaders)
- [Logs](#logs)
- [Adapters](#adapters)
- [Redis adapter](#redis-adapter)

## Usage

Expand Down Expand Up @@ -231,6 +233,41 @@ await log.setup({
});
```

## Adapters

Custom adapters can be used to broadcast packets between several Socket.IO
servers.

### Redis adapter

Documentation: https://socket.io/docs/v4/redis-adapter/

```js
import { serve } from "https://deno.land/std/http/server.ts";
import {
createRedisAdapter,
createRedisClient,
Server,
} from "https://deno.land/x/socket_io/mod.ts";

const [pubClient, subClient] = await Promise.all([
createRedisClient({
hostname: "localhost",
}),
createRedisClient({
hostname: "localhost",
}),
]);

const io = new Server({
adapter: createRedisAdapter(pubClient, subClient),
});

await serve(io.handler(), {
port: 3000,
});
```

## License

[ISC](/LICENSE)
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "3.0"
services:
redis:
image: redis:5
ports:
- "6379:6379"
48 changes: 47 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
export { Server, type ServerOptions } from "./packages/socket.io/mod.ts";
export {
Adapter,
type BroadcastOptions,
type Namespace,
Server,
type ServerOptions,
type Socket,
} from "./packages/socket.io/mod.ts";

/**
* The Redis adapter, to broadcast packets between several Socket.IO servers
*
* Documentation: https://socket.io/docs/v4/redis-adapter/
*
* Usage:
*
* ```
* import { serve } from "https://deno.land/std/http/server.ts";
* import { Server, createRedisAdapter, createRedisClient } from "https://deno.land/x/socket_io/mod.ts";
*
* const [pubClient, subClient] = await Promise.all([
* createRedisClient({
* hostname: "localhost",
* }),
* createRedisClient({
* hostname: "localhost",
* })
* ]);
*
* const io = new Server({
* adapter: createRedisAdapter(pubClient, subClient)
* });
*
* await serve(io.handler(), {
* port: 3000
* });
* ```
*/
export {
createAdapter as createRedisAdapter,
type RedisAdapterOptions,
} from "./packages/socket.io-redis-adapter/mod.ts";

/**
* Temporary export to provide a workaround for https://github.com/denodrivers/redis/issues/335
*/
export { connect as createRedisClient } from "./vendor/deno.land/x/redis@v0.27.1/mod.ts";

0 comments on commit 39eaa0e

Please sign in to comment.