使用 MQTT 的 RPC 样式请求 [英] RPC style request with MQTT

查看:60
本文介绍了使用 MQTT 的 RPC 样式请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 MQTT 实现 RPC 样式通信(同步请求/响应)的最佳方法是什么?或者使用另一个接口(例如 REST api)是否有意义?

提前致谢!

解决方案

我在我的应用程序中所做的是在我的消息中包含一个响应主题.发件人将使用 uuid 生成一个主题,然后订阅该主题.然后将发送包含响应主题的消息,接收者将发送对该主题的回复.一旦发送收到回复,它将取消订阅该主题.发送方和接收方都是异步的,但可以执行交易.

伪代码:

<前>发件人生成uuid订阅(主题=uuid)味精 = {...resp_topic: uuid}发布(主题,味精)接收者on_message主题来自发件人做一些工作发布(sender_data['resp_topic'],结果)发件人on_message主题是 resp_topic得到结果取消订阅(resp_topic)

what is the best way to implement RPC style communication (synchronous request/response) with MQTT? Or would it even make sense to put another interface (e.g. REST api) into use?

Thanks in advance!

解决方案

What I've done in my applications is include a response topic in my messages. The sender would generate a topic using a uuid then subscribe to that topic. Then would send a message containing the response topic, the receiver(s) would send a reply to this topic. Once the send receives the reply it would unsubscribe to the topic. Both sender and receiver are asynchronous and yet can perform a transaction.

Pseudo code:


sender
   generate uuid
   subscribe( topic=uuid )
   msg = {
      ... 
      resp_topic: uuid
   } 
   publish( topic, msg )

receiver
   on_message 
      topic is from sender
         do some work
         publish( sender_data['resp_topic'], result )

sender
   on_message
      topic is resp_topic
          get result
          unsubscribe( resp_topic )

这篇关于使用 MQTT 的 RPC 样式请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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