Reputation: 91
i want to set expire time to redis pub/sub messages while being published. How do i do it in nodeJS?
var redis = require('redis');
redis.createClient().publish('some channel', 'some message', function(err) {
if (err) {
console.error('error publishing:', err);
}
});
The code to pusblish a message is above. what all changes do i have to do to set expiry time for the published message.
Upvotes: 3
Views: 6267
Reputation: 111336
Every message expires immediately and you cannot change it. To make it work differently would require adding a cache of the messages, keeping them for a certain time after they are published and reposting them to any subscriber who subscribed after they were already published.
This is not how PubSub works in Redis. You can think of it as somewhat similar to events. Event listeners can listen to evets and event emitters can emit events. But there is no notion of expiration time of an event. Some listener either listens to it while it is emitted or not. The same is true for publishers and subscribers.
Upvotes: 5
Reputation: 203304
Published messages in Redis are not in any way persisted, so they don't have an expiry time.
Another consequence of this is that if there aren't any subscribers for a particular message, the message is lost.
Upvotes: 2