grivescorbett
grivescorbett

Reputation: 1625

Node.js/Express/Redis wait for only one message from channel

I have a redis connection subscribed to a channel with a get request

app.get('/wait', function (req, res) {
    redisSub.on('message', function(channel, msg) {
        console.log('rcv: ' + msg);
    });
});

And later with a different get request I send the message

app.get('/done/:msg', function (req, res) {
    redisPub.publish('message', req.params.msg);
});

The problem is that I want that request to stop listening for messages once it gets it. Or else when I go through again it will still be listening and will get the next message again. I'm not sure how to remove the listener from itself once the message is received.

Upvotes: 1

Views: 512

Answers (1)

vinayr
vinayr

Reputation: 11234

Not tested but this should work,

var callback = function(channel, msg) {
    console.log('rcv: ' + msg);
    redisSub.removeListener('message', callback); 
}
redisSub.on('message', callback);

Upvotes: 1

Related Questions