I am new to redis pub/sub. I have a chat facility in the system which is like IM. So I would like to use redis pub/sub. As I have examined the samples most of them are designed based on a chat room. In my system I will have multiple chat rooms between users like;
A:B
A:C
D:C
E:F
So, the lines above are the rooms. And I have implemented the server with node.js like 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 chat room samples redis subscriber client is created globally. And there exists only three connections all the time and that solves their problem because when a publisher publishes a message all connected clients should get it. But I have a constraint here. I want to open a chat session between two users and only these users should be the subscribers. The code above works as I would like to but I do not know if it is OK for redis to create a new subscriber client for each connection.
It would be great to hear your suggestions. Thanks in advance.