简体   繁体   中英

Do I need to run Mosquitto to interact with a remote mosquitto broker

I am new to mqtt and would like to get my head around something.

I need to get messages from (subscribe to) topics from a remote mosquitto broker. The documentation for the service says I need to run a mosquitto broker on my server.

If I understand correctly then a script that uses the mqtt gem and manages to connect using something like this:

MQTT::Client.connect(conn_opts) do |c|
  # The block will be called when you messages arrive to the topic
  c.get('test') do |topic, message|
    puts "#{topic}: #{message}"
  end
end

IS a broker? Do I need to run mosquitto on my machine or can I get away with just a script and mqtt ?

The doc describes the architecture and includes these lines:

The 3rd party platform needs an MQTT broker installed that will allow communication with the different boxes on our servers. The broker on our servers will initiate the connection and provide the credentials to allow bidirectional communication.

The architecture I have in mind is a scheduled background process, using ruby-mqtt , that will spawn, connect with the remote mosquitto server and pull down new messages in batches before finishing. Does this sound like a reasonable approach for getting messages from a remote mosquitto broker?

I have a sneaky suspicion there is something I am not getting... any help/direction will be appreciated. Thanks!

No, you do not need a local MQTT server, you can connect directly to the remote server from your ruby script.

It is typical to keep the MQTT client running all the time, and not just download periodically using cron. Although I imagine that could work, providing you are using QoS 1/2 and disable clean sessions, so that messages are retained on the remote server. Despite its name, MQTT is not a message queuing protocol, it is a publish/subscribe protocol, so it is possible at the remote server will not allow you to build up a large pool of messages.

It may however be desirable to have a local MQTT server (eg mosquitto): * You local MQTT server could deal with the storing of messages to disk until ruby is ready for them * It allows multiple local clients to receive the same message without the remote server having to send it over the network multiple times * Multiple local clients can send messages to each other, even when the remote network is down

Also be warned that ruby-mqtt doesn't support QoS 1 properly yet and also doesn't support persisting of messages or automatic reconnects, so a local mosquitto instance could solve some of those problems for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM