0

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.

georgehelou
  • 37
  • 1
  • 6

1 Answers1

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 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. – georgehelou Aug 28 '16 at 02:16
  • I just did edit my question. plz tell me what am i doing wrong @Brad – georgehelou Aug 28 '16 at 02:22
  • @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. – Brad Aug 28 '16 at 02:26
  • 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 Aug 28 '16 at 03:41
  • @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. – Brad Aug 28 '16 at 03:44
  • I edited my post. Now it includes the whole code I have. Again, sendLiveUpdates I am calling it outside this file and I am requiring it as follows: var sendLiveUpdates = require('./io').sendLiveUpdates . – georgehelou Aug 28 '16 at 03:50
  • @georgehelou I can't help you. You still didn't include how or when you're calling `sendLiveUpdates()`. This isn't your whole code. Rather than posting your code, you should do some basic debugging to see what the issue is. – Brad Aug 28 '16 at 03:52