eolas/zk/mosquitto_mqtt_client.md

1.6 KiB

tags
Linux
Debian
mqtt

Config

At /etc/mosquitto/mosquitto.conf.

Most important lines:

listener 1883
allow_anonymous true

This means mosquitto will run on port:1883 and that I am not using authentication with requests to the broker.

Websockets

In order to enable websockets, must create a config file at /etc/mosquitto/mosquitto.conf/websockets.conf:

# /etc/mosquitto/mosquitto.conf/websockets.conf`
listener 8083
protocol websockets

This is only relevant if you require access from outside of the local network.

Basic usage

Create a topic

mosquitto_sub -d -t test_topic

This then enters listening mode and takes control of terminal:

$ mosquitto_sub -d -t test_topic
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending SUBSCRIBE (Mid: 1, Topic: test_topic, QoS: 0, Options: 0x00)
Client (null) received SUBACK
Subscribed (mid: 1): 0

Publish to a topic

From my other machine I install mosquitto and:

mosquitto_pub --host 192.168.68.53 --port 1883 --topic test_topic --message 'hello world'

This registers on the broker device:

Client (null) received PUBLISH (d0, q0, r0, m0, 'test_topic', ... (11 bytes))
hello world

Topics are inherently ephemeral

Once you exit from mosquitto_sub the topic ceases to exist. You can't then relisten to the topic later as it hasn't persisted.

Topics are ephemeral routing labels, not persistent database entities. Think of them more like radio frequencies. They only exist whilst they are transmitting.