socket.emit event for only the current user doesn't fire
See original GitHub issueI’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:
- Created 8 years ago
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try using io.sockets.emit
@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.