Skip to content

Commit 65efc96

Browse files
Initial commit
0 parents  commit 65efc96

21 files changed

+6849
-0
lines changed

.github/workflows/ci.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * 0'
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
test-node:
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
matrix:
18+
node-version:
19+
- 14
20+
- 18
21+
22+
services:
23+
redis:
24+
image: redis
25+
options: >-
26+
--health-cmd "redis-cli ping"
27+
--health-interval 10s
28+
--health-timeout 5s
29+
--health-retries 5
30+
ports:
31+
- 6379:6379
32+
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v3
36+
37+
- name: Use Node.js ${{ matrix.node-version }}
38+
uses: actions/setup-node@v3
39+
with:
40+
node-version: ${{ matrix.node-version }}
41+
42+
- name: Install dependencies
43+
run: npm ci
44+
45+
- name: Run tests
46+
run: npm test

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
lib-cov
2+
*.seed
3+
*.log
4+
*.csv
5+
*.dat
6+
*.out
7+
*.pid
8+
*.gz
9+
10+
pids
11+
logs
12+
results
13+
14+
npm-debug.log
15+
node_modules
16+
.idea
17+
.nyc_output/
18+
dist/

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# History
2+
3+
# Release notes

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2023 The Socket.IO team
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Socket.IO Redis Streams adapter
2+
3+
The `@socket.io/redis-streams-adapter` package allows broadcasting packets between multiple Socket.IO servers.
4+
5+
Supported features:
6+
7+
- [broadcasting](https://socket.io/docs/v4/broadcasting-events/)
8+
- [utility methods](https://socket.io/docs/v4/server-instance/#Utility-methods)
9+
- [`socketsJoin`](https://socket.io/docs/v4/server-instance/#socketsJoin)
10+
- [`socketsLeave`](https://socket.io/docs/v4/server-instance/#socketsLeave)
11+
- [`disconnectSockets`](https://socket.io/docs/v4/server-instance/#disconnectSockets)
12+
- [`fetchSockets`](https://socket.io/docs/v4/server-instance/#fetchSockets)
13+
- [`serverSideEmit`](https://socket.io/docs/v4/server-instance/#serverSideEmit)
14+
- [connection state recovery](https://socket.io/docs/v4/connection-state-recovery)
15+
16+
Related packages:
17+
18+
- Redis adapter: https://github.com/socketio/socket.io-redis-adapter/
19+
- Redis emitter: https://github.com/socketio/socket.io-redis-emitter/
20+
- MongoDB adapter: https://github.com/socketio/socket.io-mongo-adapter/
21+
- MongoDB emitter: https://github.com/socketio/socket.io-mongo-emitter/
22+
- Postgres adapter: https://github.com/socketio/socket.io-postgres-adapter/
23+
- Postgres emitter: https://github.com/socketio/socket.io-postgres-emitter/
24+
25+
**Table of contents**
26+
27+
- [Installation](#installation)
28+
- [Usage](#usage)
29+
- [Options](#options)
30+
- [How it works](#how-it-works)
31+
- [License](#license)
32+
33+
## Installation
34+
35+
```
36+
npm install @socket.io/redis-streams-adapter redis
37+
```
38+
39+
## Usage
40+
41+
```js
42+
import { createClient } from "redis";
43+
import { Server } from "socket.io";
44+
import { createAdapter } from "@socket.io/redis-streams-adapter";
45+
46+
const redisClient = createClient({ host: "localhost", port: 6379 });
47+
48+
await redisClient.connect();
49+
50+
const io = new Server({
51+
adapter: createAdapter(redisClient)
52+
});
53+
54+
io.listen(3000);
55+
```
56+
57+
## Options
58+
59+
| Name | Description | Default value |
60+
|---------------------|--------------------------------------------------------------------|---------------|
61+
| `streamName` | The name of the Redis stream. | `socket.io` |
62+
| `maxLen` | The maximum size of the stream. Almost exact trimming (~) is used. | `10_000` |
63+
| `readCount` | The number of elements to fetch per XREAD call. | `100` |
64+
| `heartbeatInterval` | The number of ms between two heartbeats. | `5_000` |
65+
| `heartbeatTimeout` | The number of ms without heartbeat before we consider a node down. | `10_000` |
66+
67+
## How it works
68+
69+
The adapter will use a [Redis stream](https://redis.io/docs/data-types/streams/) to forward events between the Socket.IO servers.
70+
71+
Notes:
72+
73+
- a single stream is used for all namespaces
74+
- the `maxLen` option allows to limit the size of the stream
75+
- unlike the adapter based on Redis PUB/SUB mechanism, this adapter will properly handle any temporary disconnection to the Redis server and resume the stream
76+
- if [connection state recovery](https://socket.io/docs/v4/connection-state-recovery) is enabled, the sessions will be stored in Redis as a classic key/value pair
77+
78+
## License
79+
80+
[MIT](LICENSE)

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
redis:
3+
image: redis:5
4+
ports:
5+
- "6379:6379"

0 commit comments

Comments
 (0)