I sometimes get into errors of sockets undefined when trying to emit messages to sockets. I have kept a track of open sockets in an array, but still sometimes after a disconnection event is a messages is sent to it before it gets deleted in the array(like is some milliseconds, which is rare) i get this error:
io.sockets.sockets[receiverssocket2].emit('notifications',
^
TypeError: Cannot read property 'emit' of undefined
For example when a client's socket is open i send back the socket id to the client
socket.on('set username', function(userName) {
// do something here
io.sockets.sockets[socket.id].emit('setsocketid',
{"source": socket.id
});
Now here, if the client opens the socket and closes it quickly i get the undefined error. ANy way to get rid of it?