W.Lun
2 min readMar 14, 2021

A quick demo of Mosquitto with Docker

First of all, create a project directory.

> mkdir mosquitto-demo

Create folders for config, data and log

> mkdir mosquitto
> mkdir mosquitto/data mosquitto/config mosquitto/log

Create a config file

> vi mosquitto/config/mosquitto.cfg# persistence: save in-memory database to disk
persistence true
# persistence_location: location for persistent database
persistence_location /mosquitto/data/
#log_dest: log destination
log_dest file /mosquitto/log/mosquitto.log
# listener: port number of socket listener
listener 1883
# allow_anonymous: allow client connecting without username password
allow_anonymous true

Start a container

docker run -it -p 1883:1883 -p 9001:9001 -v ${PWD}/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf -v ${PWD}/mosquitto/data:/mosquitto/data -v ${PWD}/mosquitto/log:/mosquitto/log -d -it eclipse-mosquitto

Install mosquitto on host machine

> sudo snap install mosquitto

the purpose of installing mosquitto on host machine is to test if the broker inside docker is functioning

Open Terminal side by side

One for subscription, another one for publishing data

Subscribe

> mosquitto_sub -t sensors/# -q 1

subscribe to all topics under the sensors with QOS 1

Publish

> mosquitto_pub -t sensors/temperature -m 32 -q 1

You should see “32” on the other end

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response