LoginSignup
0
1

More than 1 year has passed since last update.

MQTTの動作確認

Posted at

Pub/Subモデルのmqttプロトコルの動作確認

イメージ

image.png
※Publisherがbrokerにデータを登録し、接続されているSubscriberにデータを流す。

ブローカのDockerfile

Dockerfile-broker
FROM ubuntu:20.04

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

# ブローカの起動
CMD ["mosquitto", "-c", "/etc/mosquitto/mosquitto.conf"]

各クライアントのDockerfile

Dockerfile-client
FROM ubuntu:18.04

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

docker-compose

docker-compose.yml
version: "3.7"

services:
  # ブローカ
  broker:
    build: 
      context: .
      dockerfile: Dockerfile-broker

  # publisher
  pub:
    build:
      context: .
      dockerfile: Dockerfile-client
    tty: true

  # subscriber1
  sub1:
    build:
      context: .
      dockerfile: Dockerfile-client
    tty: true

  # subscriber2
  sub2:
    build:
      context: .
      dockerfile: Dockerfile-client
    tty: true

起動

docker-compose up

下記コマンドを実行してコンテナに入る

※上からpub、sub1、sub2
image.png

subscriber側で要求をする

mosquitto_sub -h broker -t sample-topics

publisher側でデータ送信

mosquitto_pub -h broker -t sample-topics -m "send to subscriber"

subscriber側でデータを受信できているのを確認

image.png

github

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1