question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 !

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
maxzeroedgecommented, Jul 5, 2015

Try using io.sockets.emit

0reactions
flashios09commented, Jul 16, 2015

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.emit event for only the current user doesn't fire
Try using Socket.io channels to subscribe your user to when they connect, then emit to that channel to just go to that user....
Read more >
Server API - Socket.IO
Sets a modifier for a subsequent event emission that the event will only be broadcast to clients that have not joined the given...
Read more >
Emit cheatsheet - Socket.IO
to all clients in the current namespace except the sender ... WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to ...
Read more >
Rooms | Socket.IO
A room is an arbitrary channel that sockets can join and leave. It can be used to broadcast events to a subset of...
Read more >
Emitting events | Socket.IO
There are several ways to send events between the server and the client. Basic emit​. The Socket.IO API is inspired from the Node.js ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found