Answer the question
In order to leave comments, you need to log in
How to correctly send messages from redis to sockets?
I use node.js, socket.io and redis.
There is a php script that, depending on the event, throws a message into the Redis channel (pub/sub). Further, in turn, there is node.js, which is also subscribed to radish channels and works in parallel with sockets.
The task is such, for example, a post is created on the site, the message flies to the radish, node.js receives the message, and all sockets in a certain room need to send data to the client. Since the language is still asynchronous, a problem arose: How to do it right?
If you do something like this, the message flies all the socket, about as many times as there are total connected sockets.
io.on('connection', function (socket) {
...
redisClient.on("message", function (channel, message) {
message = JSON.parse(message);
console.log('---- redis');
console.log(message);
io.to(room).emit('test', {
channel: channel,
message: message
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question