Fix socket.io not working properly in single-host clusters. (#6669)

This is because we don't use the cluster module, so the master nodejs process doesn't know about any of the clients through the socket API.
This commit is contained in:
Ben Lubar
2018-07-25 10:04:18 -05:00
committed by Barış Soner Uşaklı
parent e534bf803e
commit 44373afc52
3 changed files with 64 additions and 5 deletions

View File

@@ -0,0 +1,54 @@
'use strict';
var Client = {
sendMessage: function (channel, message) {
process.send({
action: 'socket.io',
channel: channel,
message: message,
});
},
trigger: function (channel, message) {
Client.message.concat(Client.pmessage).forEach(function (callback) {
setImmediate(function () {
callback.call(Client, channel, message);
});
});
},
publish: function (channel, message) {
Client.sendMessage(channel, message);
},
// we don't actually care about which channels we're subscribed to
subscribe: function () {},
psubscribe: function () {},
unsubscribe: function () {},
unpsubscribe: function () {},
message: [],
pmessage: [],
on: function (event, callback) {
if (event !== 'message' && event !== 'pmessage') {
return;
}
Client[event].push(callback);
},
off: function (event, callback) {
if (event !== 'message' && event !== 'pmessage') {
return;
}
if (callback) {
Client[event] = Client[event].filter(function (c) {
return c !== callback;
});
} else {
Client[event] = [];
}
},
};
process.on('message', function (message) {
if (message && typeof message === 'object' && message.action === 'socket.io') {
Client.trigger(message.channel, message.message);
}
});
module.exports = Client;