Redis pubsub 消息队列,但带有回调,如 ZeroMQ [英] Redis pubsub message queue but with callback, as in ZeroMQ

查看:64
本文介绍了Redis pubsub 消息队列,但带有回调,如 ZeroMQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下代码使用ZeroMQ和Node.js实现异步消息队列(实际上没有队列,只有文件)

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); 

代码来自这里.

函数WriteFile"和DeleteFile"在代码后面定义,但没有什么特别之处.

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

函数client.send"也在另一个文件中定义,在那里定义了回调.很明显,ZeroMQ 提供了在消息传输成功时进行回调的规定.

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.

现在我想做这样的事情,但为了简单起见,使用 Redis pubsub 而不是 ZeroMQ.据我了解,node_redis 模块的发布"函数中没有回调.

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.

我的问题是,有没有办法实现这样的事情?我真的很喜欢写入文件然后在传输完成后删除它们的想法,但我希望它在 Redis 中完成.我知道我在抓着稻草,但如果有人有任何想法,我会很乐意倾听.

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.

推荐答案

redis 模块的所有命令 有一个可选的回调作为最后一个参数.

所以做类似的事情

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

应该按预期工作.

这篇关于Redis pubsub 消息队列,但带有回调,如 ZeroMQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆