How can I pop objects from Redis as they are added

2019-04-29 16:19发布

I want to get a Node.js process running as it's checking a Redis server for anything new to pop.

Another process will be doing the pushing sporadically, and the Node process will be trying to pop whatever that comes in. The Node process will stay running.

Can anyone point me to a good direction?

I'm trying to figure out how to listen for such event. Sure, I can pop it once, but how do I get Node process to keep listening for any addition to Redis server?

标签: node.js redis
3条回答
祖国的老花朵
2楼-- · 2019-04-29 16:48

You'll want to use a blocking pop: http://redis.io/commands/brpop

function waitForPush () {
  client.brpop(['list','otherlist',0], function (listName, item) {
    // do stuff
    waitForPush();
  });
}
查看更多
叛逆
3楼-- · 2019-04-29 16:51

How about a modified version of recursive function with a process.nextTick(...)

function waitForPush () {
  client.brpop(['list','otherlist',0], function (listName, item) {
    // do stuff
    process.nextTick(waitForPush);
  });
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-04-29 16:53

This seems like a good use case for pub/sub: http://redis.io/topics/pubsub

Your Node.js process that sporadically pushes to Redis can also publish to a channel each time it pushes something. Like this:

var pushClient = redis.createClient();
//push something onto Redis
pushClient.publish("pubsub-channel", "Just pushed something onto Redis");

Then your other process would subscribe to that channel. Each time the message event is fired, you pop off whatever was just pushed:

var client = redis.createClient();
client.subscribe("pubsub-channel");
client.on("message", function(channel, message){
//pop off new item
});
查看更多
登录 后发表回答