socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

socket.emit event for only the current user doesn't fire

Open flashios09 opened this issue 11 years ago • 6 comments

I'm using socket.io and when i want to emit an event from the server to client only for the connected user it doesn't work ! I don't have an error or any response :( Thats my code

// Server.js
var server     = require('http').createServer(),
    io         = require('socket.io')(server),
    logger     = require('winston'),
    port       = 1337;

// Logger config
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { colorize: true, timestamp: true });
logger.info('SocketIO > listening on port ' + port);

io.on('connection', function (socket){
    logger.info('SocketIO > Connected socket ' + socket.id);

    socket.on('ElephantIO.login', function (user){
        socket.emit('SocketIO.login', user);
    });

    socket.on('disconnect', function () {
        logger.info('SocketIO > Disconnected socket ' + socket.id);
    });
});

server.listen(port);
// Client.js
(function() {
    var socket = io.connect('http://localhost:1337');

    socket.on('SocketIO.login', function(user) {
        console.log(user);
        alert('SocketIO.login');
    });

})();

If i change the socket.emit('SocketIO.login', user); with socket.broadcast.emit('SocketIO.login', user); to emit this event to the all other connected user it works

If i change the socket.emit('SocketIO.login', user); with io.emit('SocketIO.login', user); to emit this event to the all connected user i have the same behavior of the socket.broadcast.emit('SocketIO.login', user);

Always the current user window don't have a response or an error !

flashios09 avatar Jul 04 '15 16:07 flashios09

Try using io.sockets.emit

maxzeroedge avatar Jul 05 '15 07:07 maxzeroedge

@maxzeroedge I tried io.sockets.emit, it works like io.emit or socket.broadcast.emit, always the current connected user hasn't any response or an error, the event will be fired only on the other window !

flashios09 avatar Jul 05 '15 13:07 flashios09

It worked when I was using in a separate project. Does socket.emit send an event to the client? Just for sake of checking, did you try socket.emit and socket.broadcast.emit together and get the desired response?

maxzeroedge avatar Jul 05 '15 16:07 maxzeroedge

@maxzeroedge I think that you didn't understand my problem, lets say that we have three browser window, so we have three socket A,B and C. If we want to emit an event to all three sockets A,B and C we use io.emit or io.sockets.emit If A emit an event from Client.js and we want to notify B and C we use socket.broadcast.emit If A emit an event from Client.js and we want to do something with Server.js and re-emit an event to Client.js only for A we use socket.emit not socket.broadcast.emit or io.emit|io.sockets.emit. For me when i use socket.emit from Server.js to Client.js it didn't work ! I don't have a response or an error, just nothing ! When i use socket.broadcast.emit it works, when i use io.emit socket A doesn't have a response and socket B and C have the response(like we use socket.broadcast.emit

flashios09 avatar Jul 05 '15 21:07 flashios09

I might be asking a silly question but what is 'ElephantIO.login' for, I don't see it in the client side.

sasankh avatar Jul 16 '15 00:07 sasankh

@sasankh I use ElephantIO to emit an event from a Php script. Sorry my problem isn't coming from socketIO but from my script. I emit an event then i have a redirection to another page that's why i loose(disconnected socket) the current socket and i don't have a response or an error with the current window.

flashios09 avatar Jul 16 '15 00:07 flashios09