Pregunta

I have a server like this in node.js:

socket.on('connection', function(client) {
        const subscribe = redis.createClient(6379, '127.0.0.1')

        subscribe.psubscribe('kitchen_*');

        subscribe.on("pmessage", function(pattern, channel, message) {
            client.emit(channel, message);
            log('msg', "received from channel #" + channel + " : " + message);
        });
});

In client I have like this:

socket.on('kitchen_companyName', function (data) {
        console.log('received a message: ', data);
      });

Now I want to receive the message only for kitchen_companyName in the client but even if the company name is kitchen_hello I am receiving the message. I am publishing to redis from python using pyredis.

r = redis.StrictRedis(host='localhost', port=6379)
r.publish('kitchen_'+request.user.user_company, message)
¿Fue útil?

Solución

You can try adding a condition to check the channel and emit only to that channel,

subscribe.on("pmessage", function(pattern, channel, message) {
    if(channel == "kitchen_companyName") {
      client.emit(channel, message);
      log('msg', "received from channel #" + channel + " : " + message);
    }
});

Otros consejos

Your subscription is to all channels, having "kitchen_" as prefix, watch that * in your subscription command.

So any message being published to any channel starting by "kitchen_" will come to your subscriber.

Change your subscription message to "kitchen_companyName" and you will receive only messages for that channel.

Note, that you can subscribe one client to multiple channels.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top