1

I am trying to send a message from NodeJS server to client using socket.io

However, I found the same practice all over the internet, which is wrapping the emit with io.on('connection', handler) and then making the server listen on a special "channel" event like so:

var io = require('socket.io')(); var socketioJwt = require('socketio-jwt'); var jwtSecret = require('./settings').jwtSecret; var User = require('./models/users').User; io.set('authorization', socketioJwt.authorize({ secret: jwtSecret, handshake: true })); var sockets = []; io.on('connection', function(socket) { sockets.push(socket); }); sendLiveUpdates = function(gameSession) { console.log(sockets); } exports.sendLiveUpdates = sendLiveUpdates; exports.io = io; 

My problem is: I want to emit messages outside this on connection wrapper, example from my routes or other scripts. Is it possible?

Thanks.

1

1 Answer 1

1

Yes. You just need to keep a reference to the socket.

// Just an array for sockets... use whatever method you want to reference them var sockets = []; io.on('connection', function(socket) { socket.on('event', function() { io.emit('another_event', message); }); // Add the new socket to the array, for messing with later sockets.push(socket); }); 

Then somewhere else in your code...

sockets[0].emit('someEvent'); 

What I usually do is assign new clients a UUID and add them to an object keyed by this UUID. This comes in handy for logging and what not as well, so I keep a consistent ID everywhere.

Sign up to request clarification or add additional context in comments.

7 Comments

Actually I did exactly what you suggested, but I keep on getting an empty array and I dont know why. This is my code: var sockets = []; io.on('connection', function(socket) { var userID = socket.client.request.decoded_token.id; // save user to the db sockets.push(socket.id); }); sendLiveUpdates = function(gameSession) { console.log(sockets); } exports.sendLiveUpdates = sendLiveUpdates; I am exporting sendLiveUpdates function and using it somewhere else in my scripts.
I just did edit my question. plz tell me what am i doing wrong @Brad
@georgehelou You're pushing the ID, not the socket. Push the actual socket. But, your array shouldn't be empty. Something else is wrong... you're going to have to do some basic debugging to figure out why nothing is in your array.
for some reason, whatever I do, the sockets array is always returned as empty. I guess this is because I am requiring the function and the whole file while the socket is not yet pushed to the array. Could that be possible?
@georgehelou No. As long as you call sendLiveUpdates() after you have a connection, you're fine. Earlier you had some debugging output on connection.. that's firing? It's impossible for me to guess. You didn't show the relevant code, and really this is some basic thing you're going to have to debug to proceed.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.