Allow single-host clusters to not send pubsub and socket.io messages through the database. (#6659)

* Allow single-host clusters to not send pubsub and socket.io messages through the database.

* Fix lint errors.
v1.18.x
Ben Lubar 7 years ago committed by Barış Soner Uşaklı
parent 148fc96a3a
commit c1e98eefa7

@ -95,6 +95,7 @@
"serve-favicon": "^2.4.5",
"sitemap": "^1.13.0",
"socket.io": "2.1.1",
"socket.io-adapter-cluster": "^1.0.1",
"socket.io-adapter-mongo": "^2.0.1",
"socket.io-client": "2.1.1",
"socket.io-redis": "5.2.0",

@ -36,6 +36,10 @@ Loader.init = function (callback) {
};
}
if (nconf.get('singleHostCluster')) {
require('socket.io-adapter-cluster/master')();
}
process.on('SIGHUP', Loader.restart);
process.on('SIGUSR2', Loader.reload);
process.on('SIGTERM', Loader.stop);
@ -88,6 +92,11 @@ Loader.addWorkerEvents = function (worker) {
console.log('[cluster] Reloading...');
Loader.reload();
break;
case 'pubsub':
workers.forEach(function (w) {
w.send(message);
});
break;
}
}
});

@ -1,5 +1,6 @@
'use strict';
var EventEmitter = require('events');
var nconf = require('nconf');
var real;
@ -12,9 +13,22 @@ function get() {
var pubsub;
if (nconf.get('isCluster') === 'false') {
var EventEmitter = require('events');
pubsub = new EventEmitter();
pubsub.publish = pubsub.emit.bind(pubsub);
} else if (nconf.get('singleHostCluster')) {
pubsub = new EventEmitter();
pubsub.publish = function (event, data) {
process.send({
action: 'pubsub',
event: event,
data: data,
});
};
process.on('message', function (message) {
if (message && typeof message === 'object' && message.action === 'pubsub') {
pubsub.emit(message.event, message.data);
}
});
} else if (nconf.get('redis')) {
pubsub = require('./database/redis/pubsub');
} else if (nconf.get('mongo')) {

@ -27,7 +27,13 @@ Sockets.init = function (server) {
path: nconf.get('relative_path') + '/socket.io',
});
io.adapter(nconf.get('redis') ? require('../database/redis').socketAdapter() : db.socketAdapter());
if (nconf.get('singleHostCluster')) {
io.adapter(require('socket.io-adapter-cluster')());
} else if (nconf.get('redis')) {
io.adapter(require('../database/redis').socketAdapter());
} else {
io.adapter(db.socketAdapter());
}
io.use(socketioWildcard);
io.use(authorize);

Loading…
Cancel
Save