Mosquitto MQTT Broker

install mosquitto iconMosquitto is a lightweight open source message broker that Implements MQTT versions 3.1.0, 3.1.1 and version 5.0

It is written in C by Roger Light, and is available as a free download for Windows and Linux and is an Eclipse project.

The main website is here.

MQTT Version 5.0 Support Notes:

As of release 1.6 the mosquitto broker supports MQTT v5 in addition to MQTT v3.11.

You can continue to use the older version 3.11 clienta with the latest broker.

Installing The Broker

To use it you will first need to install it.

Starting and Stopping The Broker

Depending on the install it will probably be started automatically on system startup.

Although this is very desirable in production environments it is less so in test environments.

My preferred approach is to stop the mosquitto service and start it manually from the command prompt.

This gives you access to the console which is invaluable for testing.

On Windows you can stop the service if it is running by using the control panel>admin>services.

You can also the net command:

net stop mosquitto

On Linux use:

sudo service mosquitto stop
sudo systemctl stop mosquitto.service

Starting from command line is the best option when testing and to do that use:

Windows and LInux

mosquitto -v   #start in verbose mode

To see other start options use:

mosquitto -h

By default the broker will start listening on port 1883.

You can change that by editing the configuration file-mosquitto.conf. See Quick Guide to The Mosquitto.conf File With Examples

Alternatively you can use a command line switch to specify the port e.g.

mosquitto -p 1884

You can run Multiple brokers on the same machine by starting them on different ports See this video- on running multiple brokers.

Mosquitto Client Programs

The mosquitto install includes the client testing programs.

There is a simple subscriber client

mosquitto_sub

and a publisher client

mosquitto_pub

They are useful for some quick tests.

See Using The Mosquitto_pub and Mosquitto_sub Client Tools- Examples

Common Questions and Answers

Q- Are messages stored on the broker?

A- Yes but only temporarily. Once they have been sent to all subscribers they are then discarded.

Q -Is there a limit to the message size allowed by a broker?

A- MQTT imposes a maximum message size limit of 268,435,456 bytes. You can restrict the maximum message size the broker will accept using:
message_size_limit limit
e.g
message_size_limit 1000

in the mosquitto configuration file. Messages received greater than the limit are dropped by the broker.

Q- Does the broker need a configuration file to start?

A- No

Q- What is the persistence setting in the mosquitto conf file?

A- When enabled the broker stores temporary data like persistent connections,retained messages,last will messages to a file. If the server is restarted then the values are restored.

Resources

Other Tutorials

Please rate? And use Comments to let me know more
[Total: 5    Average: 3.8/5]

11 comments

  1. Hi Steve,
    Thanks to your guide, I was able to successfully log messages coming to the broker into a text file and then using python script load the data/messages from the text file into the database.
    But I want to make this process real time, so is there a session id or something based on which I can create separate text files for the messages from broker? using which i can schedule my python script as well to run once the session expires? Or is there a better way to have the messages coming to broker be updated into the database as soon as it reaches the broker?

  2. Hi Steve,
    So I’ve been doing the python tutorials with an external broker, and am currently trying to connect to a local mosquitto broker instance but am having a bit of trouble understanding the connect() command arguments. Any ideas?

    thanks

  3. Hi Steve,
    We have subscribed to a basic package of freeboard and we are facing difficulties in linking our test mosquitto MQTT server to freeboard.
    We have successfully completed the data transfet between the TEST MOSQUITTO SERVER and OUR PLC.
    The below are the configuration settings in our PLC ( Programmable Logic Controller )

    1. TYPE : MQTT
    2. NAME : ( Any name )
    3. TOPIC : qwerty ( Advice )
    4. SERVER : 37.187.106.16 ( Advice )
    5. PORT : 1883
    6. USE ENCRYPTION : NO
    7. CLIENT ID : mqttdemo ( Advice )
    8. API KEY / USERNAME : steve
    9. PASSWORD : john
    10. JSON MASSAGES : no

    With these settings we tried these out, But the connection could not be accomplished. Please advice.

    Regards,

    Satheesh Kumaran

    1. Can you point me to the instructions you are following.You might also want to use the ask steve page as I don’t have a freeboard account and you might need to let me access using yours.
      rgds
      steve

  4. Hi Steve.
    Thanks for your publications. I recently had a question for you and your answer was very helpful. I have another one. I installed Mosquitto broker in Raspberry Pi. After a lot of problems I finally managed to have it running. I have one application (IoTView by an industrial software manufacturer) who is accessing the broker, suscribing and publishing. At the same time I have another application (Web Studio) from the same manufacturer running on a PC, acting as a server who records the information on a database. On the tests, only one of them (IoTView) could run with no problems, while the other one stop working. I have been doing modifications for several months thinking the problem was on the application. Yesterday the manufacturer let me know the problem was on the Broker running on Raspberry Pi. It could not attend two connections at the same time…. Since you know this subject more than anyone I know, I would like your opinion. Is that so? It makes no sense for me that the broker on Raspberry Pi can attend only one connection at the same time. Is there not a parameter whom I can modify to alter this? The manufacturer reccomendation was to switch and install a broker on the Server. The PC running windows.

    1. Hi
      I run Mosquitto on Pi with multiple connections. Are you using different client names for the clients? The client ID needs to be unique.
      Also start the broker with the -v option so you can see the connections and responses.
      rgds
      steve

Leave a Reply

Your email address will not be published. Required fields are marked *