翻译:https://www.hivemq.com/blog/mqtt-essentials-part2-publish-subscribe
发布/订阅模式优势
传统请求/应答模式是同步的,发布/订阅模式是异步的,这种模式带来以下几种好处:
- 时间解耦:消息发布方和订阅方不需要同时在线发布和接受消息;
- 空间解耦:消息发布方和订阅方只需要感知Broker的地址即可;
消息过滤方式
- 基于Topic过滤(mqtt使用);
- 基于消息内容过滤;
- 基于消息类型过滤;
消息消费方式
- 集群消费:一条只会投递给集群中某一台机器;
- 广播消费:一条会投递给集群中的所有机器(mqtt使用);
与MQ等消息队列区别
- 应用场景: Mqtt针对物联网场景做了很多优化,如遗嘱消息、持久会话等;
- 协议开销: Mqtt协议非常轻量级,从而消息头非常小,且方便不同语言的客户端快速开发;
The client implementation of the MQTT protocol is very straight-forward and really reduced to the essence. That’s one aspect, why MQTT is ideally suitable for small devices. MQTT client libraries are available for a huge variety of programming languages, for example Android, Arduino, C, C++, C#, Go, iOS, Java, JavaScript, .NET.
- Topic发布和订阅: Mqtt的Topic发布和订阅都是动态的,不需要提前发布和订阅;
In comparison to a message queue a topic is very lightweight. There is no need for a client to create the desired topic before publishing or subscribing to it, because a broker accepts each valid topic without any prior initialization.