0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MQTT: 異なるプロトコルで、同一のブローカーとトピックに接続

Last updated at Posted at 2025-02-14

次を確認してみました。

同一のブローカーとトピックに対して、異なるプロトコルでサブスクライブしても、
受け取るメッセージの内容は同じです。

ブローカー wss://broker.hivemq.com:8884/mqtt

トピック testaa/topic

Publish

こちらの方法で、Web で行いました。
WebブラウザでパブリックMQTTサーバーに接続

Subscribe

MQTT

BROKER="broker.hivemq.com"
#
mosquitto_sub -d -t orz -h $BROKER \
    --topic testaa/topic

MQTT over WebSocket Secure

subscribe.js
const mqtt = require('mqtt');


const brokerUrl = 'wss://broker.hivemq.com:8884/mqtt'
const topic_sub = 'testaa/topic'

// MQTT クライアントの作成
const client = mqtt.connect(brokerUrl)

client.on('connect', () => {
  console.log('Connected to MQTT broker')

  // トピックをサブスクライブ
  client.subscribe(topic_sub, (err) => {
    if (!err) {
      console.log('Subscribed to ' + topic_sub)
    }
  })
})

client.on('message', (topic, message) => {
  console.log(`Received message on ${topic}: ${message.toString()}`)
})

client.on('error', (err) => {
  console.error('Connection error:', err)
})

実行コマンド

export NODE_PATH=/usr/local/lib/node_modules
#
node subscribe.js
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?