Skip to content

Commit ff370cf

Browse files
Initial commit
0 parents  commit ff370cf

File tree

14 files changed

+4129
-0
lines changed

14 files changed

+4129
-0
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * 0'
8+
9+
jobs:
10+
test-node:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [12.x, 14.x, 16.x]
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
24+
- run: npm ci
25+
26+
- run: npm test
27+
env:
28+
CI: true
29+
timeout-minutes: 10

.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

Whitespace-only changes.

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2021 Damien Arrachequesne (@darrachequesne)
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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Socket.IO cluster adapter
2+
3+
The `@socket.io/cluster-adapter` package allows broadcasting packets between multiple Socket.IO servers.
4+
5+
![Adapter diagram](./assets/adapter.png)
6+
7+
It can be used in conjunction with [`@socket.io/sticky`](https://github.com/socketio/socket.io-sticky) to broadcast packets between the workers of the same Node.js [cluster](https://nodejs.org/api/cluster.html).
8+
9+
Supported features:
10+
11+
- [broadcasting](https://socket.io/docs/v4/broadcasting-events/)
12+
- [utility methods](https://socket.io/docs/v4/server-instance/#Utility-methods)
13+
- [`socketsJoin`](https://socket.io/docs/v4/server-instance/#socketsJoin)
14+
- [`socketsLeave`](https://socket.io/docs/v4/server-instance/#socketsLeave)
15+
- [`disconnectSockets`](https://socket.io/docs/v4/server-instance/#disconnectSockets)
16+
- [`fetchSockets`](https://socket.io/docs/v4/server-instance/#fetchSockets)
17+
- [`serverSideEmit`](https://socket.io/docs/v4/server-instance/#serverSideEmit)
18+
19+
Related packages:
20+
21+
- Postgres adapter: https://github.com/socketio/socket.io-postgres-adapter/
22+
- Redis adapter: https://github.com/socketio/socket.io-redis-adapter/
23+
- MongoDB adapter: https://github.com/socketio/socket.io-mongo-adapter/
24+
25+
**Table of contents**
26+
27+
- [Installation](#installation)
28+
- [Usage](#usage)
29+
- [License](#license)
30+
31+
## Installation
32+
33+
```
34+
npm install @socket.io/cluster-adapter
35+
```
36+
37+
## Usage
38+
39+
```js
40+
const cluster = require("cluster");
41+
const http = require("http");
42+
const { Server } = require("socket.io");
43+
const numCPUs = require("os").cpus().length;
44+
const { setupMaster, setupWorker } = require("@socket.io/sticky");
45+
const { createAdapter, setupPrimary } = require("@socket.io/cluster-adapter");
46+
47+
if (cluster.isMaster) {
48+
console.log(`Master ${process.pid} is running`);
49+
50+
const httpServer = http.createServer();
51+
52+
// setup sticky sessions
53+
setupMaster(httpServer, {
54+
loadBalancingMethod: "least-connection",
55+
});
56+
57+
// setup connections between the workers
58+
setupPrimary();
59+
60+
// needed for packets containing buffers (you can ignore it if you only send plaintext objects)
61+
// Node.js < 16.0.0
62+
cluster.setupMaster({
63+
serialization: "advanced",
64+
});
65+
// Node.js > 16.0.0
66+
// cluster.setupPrimary({
67+
// serialization: "advanced",
68+
// });
69+
70+
httpServer.listen(3000);
71+
72+
for (let i = 0; i < numCPUs; i++) {
73+
cluster.fork();
74+
}
75+
76+
cluster.on("exit", (worker) => {
77+
console.log(`Worker ${worker.process.pid} died`);
78+
cluster.fork();
79+
});
80+
} else {
81+
console.log(`Worker ${process.pid} started`);
82+
83+
const httpServer = http.createServer();
84+
const io = new Server(httpServer);
85+
86+
// use the cluster adapter
87+
io.adapter(createAdapter());
88+
89+
// setup connection with the primary process
90+
setupWorker(io);
91+
92+
io.on("connection", (socket) => {
93+
/* ... */
94+
});
95+
}
96+
```
97+
98+
## License
99+
100+
[MIT](LICENSE)

0 commit comments

Comments
 (0)