0
votes

I'll give a small premise of what I'm trying to do. I have a game concept in mind which requires multiple players sitting around a table somewhat like poker.

The normal interaction between different players is easy to handle via socket.io in conjunction with node js.

What I'm having a hard time figuring out is; I have a cron job which is running in another process which gets new information every minute which then needs to be sent to each of those players. Since this is a different process I'm not sure how I send certain clients this information.

socket.io does have information for this and I'm quoting it below:

In some cases, you might want to emit events to sockets in Socket.IO namespaces / rooms from outside the context of your Socket.IO processes. There’s several ways to tackle this problem, like implementing your own channel to send messages into the process. To facilitate this use case, we created two modules: socket.io-redis socket.io-emitter

From what I understand I need these two modules to do what I mentioned earlier. What I do not understand however is why is redis in the equation when I just need to send some messages.

Is it used to just store the messages temporarily?

Any help will be appreciated.

4

4 Answers

1
votes

There are several ways to achieve this if you just need to emit after an external event. It depend on what you're using for getting those new data to send :

/*    if the other process is an http post incoming you can use for example
 express and use your io object in a custom middleware : */

//pass the io in the req object
app.use( '/incoming', (req, res, next) => {
      req.io = io;
})  

//then you can do : 

app.post('/incoming', (req, res, next) => {
   req.io.emit('incoming', req.body);
   res.send('data received from http post request then send in the socket');
})

//if you fetch data every minute, why don't you just emit after your job : 

var job = sheduledJob('* */1 * * * *', io => {
    axios.get('/myApi/someRessource').then(data => io.emit('newData', data.data));
})
0
votes

Well in the case of socket.io providing those, I read into that you actually need both. However this shouldn't necessarily be what you want. But yes, redis is probably just used to store data temporarily, where it also does a really good job, by being close to what a message queue does.

Your cron now wouldn't need a message queue or similar behaviour.

My suggestion though would be to run the cron with some node package from within your process as a child_process hook onto it's readable stream and then push directly to your sockets.

0
votes

If the cron job process is also a nodejs process, you can exchange data through redis.io pub-sub client mechanism.

Let me know what is your cron job process in and in case further help required in pub-sub mechanism..

redis is one of the memory stores used by socket.io(in case you configure)

-1
votes

You must employ redis only if you have multi-server configuration (cluster) to establish a connection and room/namespace sync between those node.js instances. It has nothing to do with storing data in this case, it works as a pub/sub machine.