ZeroMQ - 如何在发布消息的 zeromq zmq 实现中摆脱 setTimeout? [英] ZeroMQ - How to get rid of setTimeout in zeromq zmq implementation of Publishing messages?

查看:36
本文介绍了ZeroMQ - 如何在发布消息的 zeromq zmq 实现中摆脱 setTimeout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如在发布我们使用的任何消息时 setTimeout()

For example in publishing any messages we are using setTimeout()

我尝试删除超时,但它没有发布任何消息.

I tried to remove the timeout, but it doesn't publish any message.

setTimeout(function () {
                var t = pub.send(mes + ' ' + stringObject, 0);
                }, 1000);

我正在使用 NodeJS ZMQ npm 模块.

I am using NodeJS ZMQ npm module.

在不添加 setTimeout() 函数的情况下,是否还有其他方法可以实现 PUB/SUB 模式?

Is there any other approach to PUB/SUB pattern without adding setTimeout() function?

谢谢

推荐答案

我强烈建议您阅读指南,它与许多其他问题一起回答了这个问题.

I highly recommend you read the guide, it answers this question along with many others.

需要添加 setTimeout() 的原因是因为 PUB 套接字会在 it 准备好时发送消息,它会不要等到您的订阅者准备就绪.所以,当你省略超时时会发生什么事情是这样的:

The reason you need to add the setTimeout() is because the PUB socket will send the message when it is ready, it will not wait until your subscribers are ready. So, what's going on when you omit the timeout is something like the following:

       SETUP PUB                SETUP SUB
           |                        |
          BIND                      |
           |                     CONNECT
           |                        |
     FINISH BINDING                 |
           |                        |
           |                        |
        SEND MSG1--[|               |
           |                        |
           |]--------------FINISH CONNECTING
           |                        |
        SEND MSG2----               |
           |         |              |
           |          ----          |
           |              |         |
           |               ----[RECV MSG2]
           |                        |
          ...                      ...

...您的发布者可以bind()立即,它不需要从任何网络资源发出请求.您的订阅者必须出去并与发布者建立连接,这需要时间.因此,发布者已准备好,并将 MSG1 发送到...无处可去,因为没有订阅者准备好接受它.它掉下来了,无声无息.订阅者完成连接后,它可以毫无问题地接收MSG2.

... your publisher can bind() immediately, it doesn't need to go make a request from any network resource. Your subscriber must go out and establish its connection to the publisher, which takes time. So, the publisher is ready, and sends MSG1 to... nowhere, because no subscribers are ready for it. It's dropped, silently. After the subscriber finishes connecting, it can then receive MSG2 without issue.

这样做的原因是因为 PUB/SUB 旨在在信息快速陈旧时使用 - 就像报纸一样.如果您正在制作一份报纸,而在第一天您没有任何订阅者,那么当您真正获得一些订阅者时,您将不会在第二天持有相同版本的报纸 - 您将发送他们的旧新闻.您将其废弃并为新订阅者编写另一个版本.

The reason for this is because PUB/SUB is designed to be used when information goes stale quickly - like a newspaper. If you're producing a newspaper, and on day one you don't have any subscribers, you're not going to be holding the same edition of your newspaper for the next day when you actually get some subscribers - you'd be sending them old news. You scrap it and write another edition for your new subscribers.

如果这不适合您的数据模型,那么不同的套接字配对可能更适合您.任何其他套接字类型都将等到它有一个对等方来发送它的消息.

If this doesn't fit your data model, then perhaps a different socket pairing would work better for you. Any other socket type will wait until it has a peer to send its message.

这篇关于ZeroMQ - 如何在发布消息的 zeromq zmq 实现中摆脱 setTimeout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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