So I'm trying to emit data from server to one specific client upon connecting (this is server code):
io.sockets.on('connection', function(socket){
socket.id = Math.random();
//this works, but sends data to all clients, which is not what I need
socket.emit('serverMsg');
//this seems to do nothing
io.to(socket.id).emit('serverMsg');
//this also does nothing
socket.broadcast.to(socket.id).emit('serverMsg');
});
As I've commented the lines, I can emit data to all clients, but nothing happens when I try to emit to specific client with socket.id. What am I doing wrong? There are no error messages or anything.