Redis pubsub message queue but with callback, as i

2019-07-18 21:16发布

I have found the following code that implements an asynchronous message queue (actually there is no queue, only files) with ZeroMQ and Node.js

setInterval(function() {
  var value = { id: i++, date: new Date() };
  WriteFile(value.id + ".dat", value);

  client.send(value, function(result) {
    console.log(value, result);
    DeleteFile(value.id + ".dat");
  });
}, 10000); 

The code is from here.

The functions "WriteFile" and "DeleteFile" are defined later in the code, but there is nothing extraordinary there.

The function "client.send" is also defined in another file, where the callback is defined. Clearly there is a provision from ZeroMQ to have a callback when the message transmission is successful.

Now I want to do something like this but with Redis pubsub instead of ZeroMQ for simplicity. As I understand it, there is no callback in the "publish" function from the node_redis module.

My question is, is there a way to implement something like this? I really like the idea of writing files and then deleting them whet the transmission is complete, but I would like it done in Redis. I know I am grasping at straws, but if anyone has any ideas, I will gladly listen.

1条回答
混吃等死
2楼-- · 2019-07-18 21:59

All of the redis module's commands have an optional callback as the last argument.

So doing something like

client.publish('channel', 'message', function(err) {
  if (err) throw err;
});

should work as expected.

查看更多
登录 后发表回答