1

I am using socket.io for private chatting for the server side I am using

socket.broadcast.to(receiver_socket_id).emit('message', data); // where data is a json object containing text

And at the client side code I catch the data using

socket.on('message', function (data) {
alert(data. text);
});

Its working properly and showing the alert on that specific user (socket id) ‘s panel when only two socket are connected (sender and receiver). But the problem appears when one more user connects to that socket then I see two alerts and when total 4 user connected (sender + receiver + two others) then see 3 alerts. But the good point is I can see the alerts only that specific client's panel not the others. I can’t understand the problem, please help. Please have a look on it gyazo.com/a98d3a64a9fc6487e6ded8ccd90fd5ab

it prints test three times because three browsers are opened.

Full code here: Sever side (I have used Redis):

var app = require('express')();

var server = require('http').Server(app);

var io = require('socket.io')(server);

var redis = require('redis');


server.listen(8080);
var usernames = {};
io.on('connection', function (socket) {
    console.log(socket.id);

    socket.on('adduser', function (userId) {
        usernames[userId] = socket.id;
    });

    var redisClient = redis.createClient();
    redisClient.subscribe('message-channel');
    redisClient.on('message', function (channel, data) {
        var jsonObj = JSON.parse(data);
        var rcvrId = jsonObj.rcvrId;
        socket.broadcast.to(usernames[rcvrId]).emit('message', data); // Not throwing error....should work
    });
    socket.on('disconnect', function () {
        console.log(socket.id + ' Disconnected');
        redisClient.quit();
    });
});

Client side:

var socket = io.connect('http://localhost:8080');
var userId = $('input[name="userId"]').val();
var rcvrId = $('input[name="rcvrId"]').val();

socket.on('connect', function () {
    // call the server-side function 'adduser' and send one parameter (value of prompt)
    socket.emit('adduser', userId);
});
socket.on('message', function (data) {
    data = jQuery.parseJSON(data);
    console.log(data);
    $("#messages").append("<div><strong>" + data.userId + " : </strong><span>" + data.message + "</span></div>");

});
murad
  • 175
  • 2
  • 14
  • 1
    If you want to emit event on specific socketId, then use `io.sockets.connected[receiver_socket_id].emit('message', data)` instead of `socket.broadcast.to`, because `socket.broadcast.to` broadcasts to all sockets in the given room. http://stackoverflow.com/questions/6873607/socket-io-rooms-difference-between-broadcast-to-and-sockets-in – Mukesh Sharma Dec 07 '16 at 07:50
  • http://stackoverflow.com/a/40859999/3288890 check this exactly what u need – Adiii Dec 07 '16 at 07:52
  • Possible duplicate of [private chat with socket.io](http://stackoverflow.com/questions/40859484/private-chat-with-socket-io) – Krzysztof Safjanowski Dec 07 '16 at 07:53
  • Thank you @Mukesh Sharma but it is showing 3 alerts for 3 connected user. Where socket.broadcast.to was showing 2 alerts. Thank in advance.. – murad Dec 07 '16 at 08:03
  • Thank you so much @Adiii but it is little bit different process than the current one. Can't we do this using receiver's specific socket id? Thanks in advance – murad Dec 07 '16 at 08:11
  • io.to(socket.id).emit("event",{res:'hi its work'}); – Adiii Dec 07 '16 at 08:15
  • Thank you @Adiii I have tried it first then socket.broadcast.to Both aren't working.. Please have a look on this https://gyazo.com/a98d3a64a9fc6487e6ded8ccd90fd5ab I am sending test message once but it is showing three times. Because I have three browsers opened connected with the socket. – murad Dec 07 '16 at 08:21
  • its becox u emit event to all contect user. dear u need chatroom to work weel – Adiii Dec 07 '16 at 08:27
  • Okay but I have searched a lot about private chat and got that io.to(socket.id).emit("event", data); means emitting event to that specific socket.id not all connected user. Am I wrong? – murad Dec 07 '16 at 08:32

1 Answers1

0

You can use io.of('/').sockets[rcvrId].emit('message', data). In case you are using a different namespace just replace the / with your namespace.

franckstifler
  • 396
  • 2
  • 11
  • Sorry but it is also not working. I think I am not using different namespace. Please have a look on it https://gyazo.com/a98d3a64a9fc6487e6ded8ccd90fd5ab it prints test three times because three browsers are opened. – murad Dec 07 '16 at 08:47