1

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.

1 Answer 1

1

Not tested but this should work,

var callback = function(channel, msg) {
    console.log('rcv: ' + msg);
    redisSub.removeListener('message', callback); 
}
redisSub.on('message', callback);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.