How to create redis pub / sub for instant messaging?

I am new to redis pub / sub. I have a chat on a system that is similar to IM. Therefore, I would like to use redis pub / sub. Since I reviewed the samples, most of them are chat-based. On my system, I will have several chats between users, for example:

A:B A:C D:C E:F 

So the lines above are the rooms. And I implemented a server with node.js as shown below;

 var store = redis.createClient(); var pub = redis.createClient(); io.sockets.on('connection', function (socket) { var sub = redis.createClient(); sub.on("message", function(pattern, data){ data = JSON.parse(data); socket.send(JSON.stringify({ type: "chat", key: pattern, nick: data.nickname, message: data.text })) } }); socket.on('message', function (messageData) { store.incr("messageNextId", function(e, messageId) { var room = "" var from = messageData.clientId > socket.nickname ? socket.nickname : messageData.clientId; var to = messageData.clientId < socket.nickname ? socket.nickname : messageData.clientId; room = from + ":" + to; var message = { id: messageId, nickname: socket.nickname, text: messageData.text }; store.rpush("rooms:" + room, JSON.stringify(message), function(e, r) { pub.publish(room, JSON.stringify(message)) }); }); }); 

As you can see, I am creating a new redis subscriber for each connection. In other sample chats, client-client redis is created globally. And there are only three connections all the time, and this solves their problem, because when the publisher publishes a message, all connected clients must receive it. But I have a limitation. I want to open a chat session between two users, and only these users should be subscribers. The code above works as I would like, but I do not know if it is normal for redis to create a new client-subscriber for each connection.

It would be great to hear your suggestions. Thank you in advance.

+14
redis publish-subscribe
Apr 13 2018-12-12T00:
source share
1 answer

As always, you need to compare things like this for your own use case - it’s impossible to give general recommendations. You may need to increase the maximum number of open files on your system, both in a system-wide and redis user. This also applies to the user who runs your web server, of course.

However, you should definitely listen to the socket.on('disconnect') and quit() when the user leaves. You may also be interested to know that socket.io has a redis server that uses redis pub / sub and also has a room concept, so you can save yourself some trouble using this, since you are already dependent on the .io socket.

Edit: After a quick check, I get this error message from Redis after 991 subscribers:

 Ready check failed: Error: Error: ERR max number of clients reached 

Here is the default redis.conf :

 # Set the max number of connected clients at the same time. By default # this limit is set to 10000 clients, however if the Redis server is not # able ot configure the process file limit to allow for the specified limit # the max number of allowed clients is set to the current file limit # minus 32 (as Redis reserves a few file descriptors for internal uses). # # Once the limit is reached Redis will close all the new connections sending # an error 'max number of clients reached'. # # maxclients 10000 

My system (Ubuntu 11.11) has a default nofile of 1024, so my quick test should fail after 992 connected clients, which seems to be straight from the test (I also have one client for the publisher). My suggestion for you is to check your nofile limit (on my system, it is in /etc/security/limits.{conf,d/*} and setting up redis maxclients , and then in the control, reference, reference!

+20
Apr 13 2018-12-14T00:
source share



All Articles