Skip to content

Commit 8c29a7f

Browse files
feat: add support for Socket.IO cluster
1 parent 590f044 commit 8c29a7f

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/God.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ var timesLimit = require('async/timesLimit');
2727
var Configuration = require('./Configuration.js');
2828
var semver = require('semver');
2929

30+
const http = require('http');
31+
const { setupMaster } = require('@socket.io/sticky');
32+
const { setupPrimary } = require('@socket.io/cluster-adapter');
33+
3034
/**
3135
* Override cluster module configuration
3236
*/
@@ -39,7 +43,8 @@ if (semver.lt(process.version, '10.0.0')) {
3943
else {
4044
cluster.setupMaster({
4145
windowsHide: true,
42-
exec : path.resolve(path.dirname(module.filename), 'ProcessContainer.js')
46+
exec : path.resolve(path.dirname(module.filename), 'ProcessContainer.js'),
47+
serialization: 'advanced', // for packets containing buffers (https://nodejs.org/api/child_process.html#child_process_advanced_serialization)
4348
});
4449
}
4550

@@ -82,6 +87,16 @@ God.init = function() {
8287
setTimeout(function() {
8388
God.Worker.start()
8489
}, 500)
90+
91+
const httpServer = http.createServer();
92+
93+
setupMaster(httpServer, {
94+
loadBalancingMethod: 'least-connection',
95+
});
96+
97+
setupPrimary();
98+
99+
httpServer.listen(process.env.PM2_PORT || 8080);
85100
}
86101

87102
God.writeExitSeparator = function(pm2_env, code, signal) {

lib/God/ClusterMode.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ module.exports = function ClusterMode(God) {
5757
* Broadcast message to God
5858
*/
5959
clu.on('message', function cluMessage(msg) {
60+
if (msg && msg.source === '_sio_adapter') {
61+
// ignore messages from the @socket.io/cluster-adapter (https://github.com/socketio/socket.io-cluster-adapter)
62+
return;
63+
}
6064
/*********************************
6165
* If you edit this function
6266
* Do the same in ForkMode.js !

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "pm2",
2+
"name": "@socket.io/pm2",
33
"preferGlobal": true,
44
"version": "5.1.0",
55
"engines": {
@@ -170,6 +170,8 @@
170170
"@pm2/io": "~5.0.0",
171171
"@pm2/js-api": "~0.6.7",
172172
"@pm2/pm2-version-check": "latest",
173+
"@socket.io/cluster-adapter": "^0.1.0",
174+
"@socket.io/sticky": "^1.0.0",
173175
"async": "~3.2.0",
174176
"blessed": "0.1.81",
175177
"chalk": "3.0.0",

0 commit comments

Comments
 (0)