Skip to content

Commit 0ae7636

Browse files
Merge remote-tracking branch 'socket.io-cluster-adapter/monorepo'
2 parents cf6816a + 27fd420 commit 0ae7636

File tree

14 files changed

+5605
-0
lines changed

14 files changed

+5605
-0
lines changed

package-lock.json

Lines changed: 547 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"packages/socket.io-cluster-engine",
99
"packages/engine.io-client",
1010
"packages/socket.io-adapter",
11+
"packages/socket.io-cluster-adapter",
1112
"packages/socket.io-parser",
1213
"packages/socket.io-client",
1314
"packages/socket.io",
@@ -73,6 +74,7 @@
7374
"ts-loader": "^9.5.1",
7475
"ts-node": "^10.9.2",
7576
"tsd": "^0.31.1",
77+
"tsx": "~4.20.6",
7678
"typescript": "^5.5.3",
7779
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.48.0",
7880
"wdio-geckodriver-service": "^5.0.2"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# History
2+
3+
- [0.2.2](#022-2023-03-24) (Mar 2023)
4+
- [0.2.1](#021-2022-10-13) (Oct 2022)
5+
- [0.2.0](#020-2022-04-28) (Apr 2022)
6+
- [0.1.0](#010-2021-06-22) (Jun 2021)
7+
8+
9+
10+
# Release notes
11+
12+
## [0.2.2](https://github.com/socketio/socket.io-cluster-adapter/compare/0.2.1...0.2.2) (2023-03-24)
13+
14+
The `socket.io-adapter` package was added to the list of `peerDependencies`, in order to fix sync issues with the version imported by the socket.io package (see [15fd56e](https://github.com/socketio/socket.io-cluster-adapter/commit/15fd56e78d52aa65c5fbf412dec57ab4bdaee7cc)).
15+
16+
Support for connection state recovery (see [here](https://github.com/socketio/socket.io/releases/4.6.0)) will be added in the next release.
17+
18+
19+
20+
## [0.2.1](https://github.com/socketio/socket.io-cluster-adapter/compare/0.2.0...0.2.1) (2022-10-13)
21+
22+
23+
### Bug Fixes
24+
25+
* properly handle ERR_IPC_CHANNEL_CLOSED errors ([#6](https://github.com/socketio/socket.io-cluster-adapter/issues/6)) ([be0a0e3](https://github.com/socketio/socket.io-cluster-adapter/commit/be0a0e3217bd7100d569e5624194612bcc8b96ff))
26+
27+
28+
29+
## [0.2.0](https://github.com/socketio/socket.io-cluster-adapter/compare/0.1.0...0.2.0) (2022-04-28)
30+
31+
32+
### Features
33+
34+
* broadcast and expect multiple acks ([055b784](https://github.com/socketio/socket.io-cluster-adapter/commit/055b7840d8cf88173d8299041ef3fafa9791c97a))
35+
36+
This feature was added in `socket.io@4.5.0`:
37+
38+
```js
39+
io.timeout(1000).emit("some-event", (err, responses) => {
40+
// ...
41+
});
42+
```
43+
44+
Thanks to this change, it will now work within a Node.js cluster.
45+
46+
47+
48+
## 0.1.0 (2021-06-22)
49+
50+
Initial commit
51+
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.
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)