MQTT is generally faster than AMQP due to its lightweight design and focus on publish/subscribe messaging.

In general, MQTT can be faster than TCP in certain situations.

  • Lightweight Protocol: MQTT is a lightweight protocol that uses a small header and a simple message format, reducing network overhead.
  • Persistent Connection: MQTT uses a persistent connection between the client and server, reducing the overhead of establishing and closing connections.
  • Asynchronous Messaging: MQTT enables asynchronous messaging, meaning the client can send messages without waiting for the server to respond.
  • Optimized for IoT: MQTT is specifically designed for IoT and M2M applications, where speed and efficiency are crucial.
  • TCP guarantees packet delivery, which may cause delays in data transmission.

The commands are mosquitto, mosquitto_sub, mosquitto_pub, to start mosquitto, subscribe to a topic to read messages, and to publish a message to a topic.

We will only use 3 commands, which come in the mosquitto utility package.

On Mac you can install it:

brew install mosquitto

You must first start Mosquitto on a terminal.

mosquitto

In another terminal I hear a topic

mosquitto_sub -t <topic> -v

The mosquitto_sub command is a command-line tool used to subscribe to a topic on an MQTT broker and receive messages.

In another terminal I send a message to the topic

The mosquitto_pub command is a command-line tool used to publish messages to an MQTT broker.

mosquitto_pub -h <host> -p <port> -t <topic> -m "<message>"

all together:


use online

It is possible to run mosquitto_sub online using a public MQTT broker

broker.hivemq.com: A public MQTT broker provided by HiveMQ.

If you want to connect to the HiveMQ public MQTT broker, you can use the following command:

mosquitto_sub -h broker.hivemq.com -t a -v

a is the topic

listen the “a” topic:

mosquitto_sub -h broker.hivemq.com -t a -v

-h broker.hivemq.com: This option specifies the host of the MQTT broker you want to connect to. In this case, the public HiveMQ broker (broker.hivemq.com) is being used.

-t a: This option specifies the topic you want to subscribe to. In this case, you are subscribing to topic a.

-v: This option enables verbose mode, which displays additional information about received messages, including the message topic and payload.


In the context of messaging and communication, a broker is a server that acts as a central point for receiving, processing, and delivering messages.

  • Message Reception: A broker can receive messages from different sources, such as devices or applications.
  • Message Storage: A broker can temporarily store messages until they are delivered to their destination.
  • Message Routing: A broker can route messages to their correct destination based on the provided routing information.
  • Queue Management: A broker can manage message queues, allowing messages to be stored and delivered in an orderly manner.
  • Security and Authentication: A broker can provide security and authentication to ensure that only authorized devices or applications can send and receive messages.
  • MQTT Broker: An MQTT broker is a server that implements the MQTT (Message Queue Telemetry Transport) protocol for communication between devices and applications.