MQTT--mosquitto use detailed explanation

Usage of mosquitto_pub (publish)

usage:

mosquitto_pub [-d] [-h hostname] [-i client_id] [-I client id prefix] [-p port number] [-q message QoS] [--quiet] [-r] { -f file | -l | -m message | -n | -s} [-u username [-P password] ] [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] -t message-topic

  

Options:

-d, --debug  : enable debug option

-f, --file: Send the content of a file as the content of the message. After testing, it supports txt files, but does not support other forms of files such as doc.

-h, –host: indicates the domain name to connect to, the default is localhost

-i, –id: The ID number of the client, if not specified, the default is mosquitto_pub_ plus the process id of the client, which cannot be used at the same time as –id_prefix.

-I, --id-prefix  : Specifies the prefix of the client ID, which is connected with the client's process ID to form the client's ID. It cannot be used with --id at the same time.

-l, --stdin-line: read input from the total segment to send messages, one line at a time, blank lines will not be sent.

-m, --message: Send a message from the command line, -m followed by the content of the message sent.

-n, --null-message: Send an empty message.

-p, --port: The port number of the connection, the default is 1883.

-P, –pw: Specify a password for proxy authentication, a valid username is required when using this option.

-q, --qos: Specify the quality of service of the message, which can be 0, 1, 2, and the default is 0.

--quiet: If this option is specified, no errors will be printed, which of course excludes error messages caused by invalid user input.

-r, --retain: If this option is specified, the message will be retained as the last received message. The next subscriber to the message will receive at least this message.

-s, --stdin-file: Receive transmitted message content from standard input, all input sent as one message.

-t, --topic: Specifies the topic to which the message is published.

-u, --username: Specify the username for proxy authentication.

--will-payload: If this option is specified, in case the client accidentally disconnects from the proxy server, the message will be kept on the server and sent out. This option must also specify the topic with --will-topic.

--will-qos: Specifies Will's quality of service, the default is 0. Must be used with the option --will-topic.

--will-retain: If this option is specified, in case the client disconnects unexpectedly, messages that have been sent will be treated as retained messages. Must be used with option --will-topic.

--will-topic: Specifies the topic to which Will messages are sent when the client disconnects unexpectedly.

 

Usage of mosquitto_sub (subscription)

usage:

mosquitto_sub [-c] [-d] [-h hostname] [-i client_id] [-I client id prefix] [-k keepalive time] [-p port number] [-q message QoS] [--quiet] [-v] [ -u username [-Ppassword] ] [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] -t message topic ...  

  

Order:

mosquitto_sub subscribes to a topic and prints options when a message is received : 

-c, --disable-clean-session: Disable the 'clean session' option, that is, if the client disconnects, the subscription is still reserved to receive subsequent messages with QoS 1 and 2. When the client reconnects, It will receive messages that are already queued. When this option is recommended, the client id option is set to –id

-d, --debug: enable debug option

-h, –host: indicates the domain name to connect to, the default is localhost

-i, –id: The ID number of the client, if not specified, the default is mosquitto_pub_ plus the process id of the client, which cannot be used at the same time as –id_prefix.

-I, --id-prefix: Specifies the prefix of the client ID, which is connected with the client's process ID to form the client's ID. You cannot use –id at the same time.

-k, --keepalive: The interval between sending the PING command to the proxy (the purpose is to inform the proxy that the client connection is maintained and working normally), the default is 60s

-p, --port: indicates the port the client is connected to, the default is 1883

-P, –pw: Specify a password for proxy authentication, a valid username is required when using this option.

-q, --qos: Specify the quality of service of the message, which can be 0, 1, 2, and the default is 0.

--quiet: If this option is specified, no errors will be printed, which of course excludes error messages caused by invalid user input.

-t, --topic: Specifies the message topic to subscribe to, allowing subscription to multiple topics at the same time

-u, --username: Specify the username for proxy authentication.

-v, --verbose: verbosely print received messages. If this option is specified, the topic name - "topic message content" will be printed in front of the message, otherwise, only the message content will be printed

--will-payload: If this option is specified, in case the client accidentally disconnects from the proxy server, the message will be kept on the server and sent out. This option must also specify the topic with --will-topic.

--will-qos: Specifies Will's quality of service, the default is 0. Must be used with the option --will-topic.

--will-retain: If this option is specified, in case the client disconnects unexpectedly, messages that have been sent will be treated as retained messages. Must be used with option --will-topic.

--will-topic: Specifies the topic to which Will messages are sent when the client disconnects unexpectedly.

Type of message

1. Connection request (CONNECT)

When a TCP/IP socket connection is established from a client to a server, a connection stream must be used to create a protocol-level session.

2. Connection request confirmation (CONNECTACK)

The connection request confirmation message (CONNECTACK) is sent by the server to the client to confirm the connection request of the client

3. Publish the message (PUBLISH)

The client publishes the message to the server to provide it to subscribers with different needs. Each published message has a topic, which is a hierarchical namespace that defines the source classification of the message, so that subscribers can subscribe to the topics they need. Subscribers can register their desired message classes.

4. Publish a confirmation message (PUBACK)

A Publish Acknowledgement message (PUBACK) is a response to a Published message with a quality of service level of 1. It can be the message acknowledgment of the server to the client that publishes the message, or the response of the message subscriber to the server that publishes the message.

5. Publish Confirmation Message (PUBREC)

A PUBREC message is a response to a Published message with a quality of service level of 2. This is the second packet of a protocol flow with a quality of service level of 2. PUBREC is the response of the server to the client that publishes the message, or the response of the message subscriber to the server that publishes the message.

6. Publish a confirmation message (PUBREL)

PUBREL is the acknowledgment of the PUBREC message from the server by the message publisher, or the acknowledgment of the server by the PUBREC message from the message subscriber. It is the third packet of a protocol flow with a quality of service level of 2.

7. Confirm release complete (PUBCOMP)

The PUBCOMP packet is the server's response to the PUBREL packet of the packet publisher, or the packet subscriber's response to the server's PUBREL packet. It is the fourth and last packet of a protocol flow with quality of service level 2.

8. Subscribe to a named topic (SUBSCRIBE)

The SUBSCRIBE message allows a client to register with the server one or more topic names of interest. Messages published to these topics are delivered from the server to the client as publish messages. The subscription message also describes the quality of service level that the subscriber wants to receive for the published message.

9. Subscription message confirmation (SUBACK)

When the server receives the subscription message sent by the client, it will send the confirmation message of the subscription message to the client. One such acknowledgment message contains a list of the QoS classes granted. The order of the QoS classes granted corresponds to the order of the topic names in the corresponding subscription message.

10. Unsubscribe from named topics (UNSUBSCRIBE)

The message to unsubscribe from the topic is sent from the client to the server to unsubscribe from the named topic.

11. Unsubscribe confirmation (UNSUBACK)

The unsubscribe confirmation message is sent from the server to the client to confirm the unsubscribe request message sent by the client.

12. Ping request (PINGREQ)

The Ping request message is sent from the connected client to the server to ask whether the server still exists.

13.Ping response (PINGRESP)

The ping response message is sent from the server to the client of the ping request to confirm the ping request of the client.

14. Disconnect notification (DISCONNECT)

The disconnect notification message is sent from the client to the server to indicate that its TCP/IP connection is about to be closed, and it allows a complete disconnection, rather than just going offline. If the client has contacted the clean session flag set, then all previous information about client maintenance will be discarded. A server MUST NOT rely on the client to close the TCP/IP connection after receiving a disconnect message.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023697&siteId=291194637