Run Mosquitto with Docker and try WebSocket communication with MQTT

Create a Dockerfile and Mosquitto configuration file.

Dockerfile


FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -y mosquitto mosquitto-clients

COPY ./mosquitto.conf /etc/mosquitto/mosquitto.conf

# MQTT Port
EXPOSE 1883

# WebSocket Port
EXPOSE 8080

CMD ["mosquitto", "-c", "/etc/mosquitto/mosquitto.conf"]

mosquitto.conf


# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

listener 1883

listener 8080
protocol websockets

Build the image and start the container.

$ docker build -t mosquitto_image .
$ docker run -d -p 1883:1883 -p 8080:8080 --name mosquitto_container mosquitto_image

Try pub / sub from the MQTT client.

$ mosquitto_pub -h localhost -p 1883 -d -t hello_topic -m "hello"
Client mosq-8zgvruDJ1gCOBNFchP sending CONNECT
Client mosq-8zgvruDJ1gCOBNFchP received CONNACK (0)
Client mosq-8zgvruDJ1gCOBNFchP sending PUBLISH (d0, q0, r0, m1, 'hello_topic', ... (5 bytes))
Client mosq-8zgvruDJ1gCOBNFchP sending DISCONNECT

$ mosquitto_sub -h localhost -p 1883 -d -t hello_topic
Client mosq-GP0NmJL6vHdSMuuhF7 sending CONNECT
Client mosq-GP0NmJL6vHdSMuuhF7 received CONNACK (0)
Client mosq-GP0NmJL6vHdSMuuhF7 sending SUBSCRIBE (Mid: 1, Topic: hello_topic, QoS: 0, Options: 0x00)
Client mosq-GP0NmJL6vHdSMuuhF7 received SUBACK
Subscribed (mid: 1): 0
Client mosq-GP0NmJL6vHdSMuuhF7 received PUBLISH (d0, q0, r0, m0, 'hello_topic', ... (5 bytes))
hello

Try pub / sub from MQTT over WebSocket as well.

https://mitsuruog.github.io/what-mqtt/

image.png

that's all.

Recommended Posts

Run Mosquitto with Docker and try WebSocket communication with MQTT
Run logstash with Docker and try uploading data to Elastic Cloud
Create jupyter notebook with Docker and run ruby
Run Pico with docker
Try WebSocket with jooby
Try WildFly with Docker
Run TAO Core with Docker
Run Rails whenever with docker
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
Run lambda with custom docker image
Run SQL Server with Docker ToolBox
Try running cloudera manager with docker
Hello World with Docker and C
Try bidirectional communication with gRPC Java
Make JupyterLab run anywhere with docker
Microservices With Docker and Cloud Performance
Try running MySql and Blazor with docker-compose
Try running Slack's (Classic) Bot with docker
Communicate Gitlab and Gitlab Runner launched with Docker
Try using Kong + Konga with Docker Compose.
Run Ubuntu + ROS with Docker on Mac
How to run Blazor (C #) with Docker
Try local development of AWS (S3, DynamoDB) with AWS SDK for JavaScript and Docker
Try building Express + PostgreSQL + Sequelize with Docker [Part 2]
Experience .NET 5 with Docker and Visual Studio Code
Try running MPLS-VPN with FR Routing on Docker
Try to link Ruby and Java with Dapr
Try running OSPF with FR Routing on Docker
Run JSP Hello World with Tomcat on Docker
Try building Express + PostgreSQL + Sequelize with Docker [Part 1]
Try drawing a cube with View and Layer
Try using another Servlet container Jetty with Docker