I currently have a one-to-one chat application in node.js and socket.io. as the users on my website grow, I want to introduce redis in my chat application. Here is a small example of my current application:
// all requires and connections io.sockets.on('connection', function(socket) { socket.on('message', function(msg) { // code to get receiverssocket io.sockets.sockets[receiverssocket].emit('message', {"source": msg.sendersusername,"msg": msg.msg}); }); });
Now I am trying to find examples of how to do this using redis, but I can not find an example of a single chat with redis. I can only find examples in which messages are sent to all users. Here is one example I looked at
http://garydengblog.wordpress.com/2013/06/28/simple-chat-application-using-redis-socket-io-and-node-js/
One way to do this is to create channels for each user receiving messages, but this will lead to thousands of channels. Any help on how I can do this?
Edit: code added
io.sockets.on('connection', function (client) { sub.on("message", function (channel, message) { console.log("message received on server from publish "); client.send(message); }); client.on("message", function (msg) { console.log(msg); if(msg.type == "chat"){ pub.publish("chatting." + msg.tousername,msg.message); } else if(msg.type == "setUsername"){ sub.subscribe("chatting." + msg.user); sub.subscribe("chatting.all" ); pub.publish("chatting.all","A new user in connected:" + msg.user); store.sadd("onlineUsers",msg.user); } }); client.on('disconnect', function () { sub.quit(); pub.publish("chatting.all","User is disconnected :" + client.id); }); });
source share