one to one chat app with node.js, socket.io and redis
Asked Answered
M

1

7

I currently have a chat application(one to one) in node.js and socket.io. as the users in my website are increasing, i want to introduce redis in my chat app. Here is a small example of my current app:

// 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 with redis, but i cannot find an example of one to one chats with redis. i can only find examples in which messages are sent to all the users. Here is one of the examples i looked at

http://garydengblog.wordpress.com/2013/06/28/simple-chat-application-using-redis-socket-io-and-node-js/

One way of i thought of doing this was to create channels for each user receiving messages, but that would result in thousands of channels. Any help on how i can do this?

Edit: added some code

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);
});

});
Mal answered 25/8, 2014 at 9:59 Comment(0)
K
2

You have to publish on a dedicated User channel. I think that there is no other way. But don't worry, pub/sub channels are volatile, so that should work well.

Instead of publishing to chatting, publish your message on chatting.username, and subscribe to both.

io.sockets.on('connection', function (client) {

    sub.subscribe("chatting." + client.id);
    sub.subscribe("chatting.all" );

    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);
        // supose that msg have a iduserto that is the distant contact id
        if(msg.type == "chat") {
            pub.publish("chatting." + msg.iduserto,msg.message);
        }
        else if(msg.type == "setUsername") {
            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);
    });
});
Kail answered 25/8, 2014 at 14:2 Comment(3)
Hi.. after looking at your code i tried to change the code. i have added the code above. but this code sends the message to everyone, and not an individual. you can check the resulting application here smartican.com/ajax/redis.htmlMal
how do i make a single username subscribe to a single channel?Mal
this code user subscribe to his channel and to the global channel. you have to change code for exemple the message to send can have the id of the other user to talking to , i change the code. if user1 send user2 a message , in this code user3 should not receive it , but every user recieve the loggin and loggout messageKail

© 2022 - 2024 — McMap. All rights reserved.