-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CPU consumption #41
Comments
I switched to using my own redis-pubsubber. |
👍 |
Hello, I have the same issue in a project in which many namespaces are used with multiple Node.js instances. @peteruithoven Could you explain how did you switch from socket.io-redis to your redis-pubsubber package in a multi-instances Socket.io app ? |
This is basicly how I do it: var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redisPubSub = require('redis-pubsubber')('myproject',6379,'localhost');
redisPubSub.on('error',function(err) {
console.log("myproject namespaces redis error: ",err);
});
createNSP("mynamespace");
function createNSP(nspName) {
var nsp = io.of("/"+nspName);
// create pub/sub channel for this namespace
var eventsChannel = redisPubSub.createChannel(nspName);
nsp.on('connection', function(socket){
// receive pub/sub events
function onEventsMessage() {
// emit incomming pub/sub broadcast to this socket
socket.emit.apply(socket,arguments);
// do something
}
// make all new socket connections listen to pub/sub messages
eventsChannel.on("message", onEventsMessage);
socket.on("eventToBroadcast",function() {
// broadcast event using pub/sub
// using apply to forward all arguments
eventsChannel.publish.apply(eventsChannel,arguments);
});
socket.on('disconnect', function(){
eventsChannel.removeListener("message",onEventsMessage);
});
}
} To forward all incomming messages I also use the following trick: But, as you can see, this is not at all a drop in replacement.
But I'm not sure if you can do this per namespace (instead of socket.io globally) |
The same goes for the rooms, even if there is no client joined to them the socket.io server will still receive all the messages for that room from redis. It is because socket.io-redis subscribes for all messages with the prefix (socket.io by default). With the current design it is needed, because messages are sent to channels prefix + '#' + clientId, no namespace or room in the channel name. So messages from different namespaces will be received from redis. sub.psubscribe(prefix + '#*', function(err){
if (err) self.emit('error', err);
}); You can test it with this repo. In pull request #46 I changed the channel names to prefix + '#' + namespance + '#' [+ room + '#'], with this socket.io servers can subscribe to the necessary channels only. |
This issue has been resolved in version |
I'm developing a project where we have multiple Node.js instances and we use socket.io-redis to broadcast to all clients of all instances. This works but it seems to consume lot's of CPU.
I've done a couple of tests:
I'm using a "stresstest" where I create 100 namespaces with each 2 clients and where one client broadcasts something to the other client, every second (with {state: 'mystate'} as data).
(I'm using PM2 to start multiple instances and it's monitor functionality to monitor the cpu)
I've used the debug module to see what's going on on the other instances when I use socket.io-redis and I see it's basicly lot's of
socket.io-redis ignore different namespace
.I think socket.io-redis isn't optimized for "lot's" of namespaces?
The text was updated successfully, but these errors were encountered: