Skip to content

Commit 94e9a43

Browse files
feat: add support for Socket.IO cluster
1 parent c7a84c2 commit 94e9a43

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
@@ -26,12 +26,17 @@ var cst = require('../constants.js');
2626
var timesLimit = require('async/timesLimit');
2727
var Configuration = require('./Configuration.js');
2828

29+
const http = require('http');
30+
const { setupMaster } = require('@socket.io/sticky');
31+
const { setupPrimary } = require('@socket.io/cluster-adapter');
32+
2933
/**
3034
* Override cluster module configuration
3135
*/
3236
cluster.setupMaster({
3337
windowsHide: true,
34-
exec : path.resolve(path.dirname(module.filename), 'ProcessContainer.js')
38+
exec : path.resolve(path.dirname(module.filename), 'ProcessContainer.js'),
39+
serialization: 'advanced', // for packets containing buffers (https://nodejs.org/api/child_process.html#child_process_advanced_serialization)
3540
});
3641

3742
/**
@@ -73,6 +78,16 @@ God.init = function() {
7378
setTimeout(function() {
7479
God.Worker.start()
7580
}, 500)
81+
82+
const httpServer = http.createServer();
83+
84+
setupMaster(httpServer, {
85+
loadBalancingMethod: 'least-connection',
86+
});
87+
88+
setupPrimary();
89+
90+
httpServer.listen(process.env.PM2_PORT || 8080);
7691
}
7792

7893
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.3.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)