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: 14   Average: 4.4/5]

55 comments

  1. Hi
    It seems like Mosquitto will not read the Mosquitto database used for persisting when starting up. I have configured the following for Mosquitto:

    persistence true
    persistence_file mosquitto.db
    persistence_location /tmp/

    I publish a few topics with QoS 2 and retained. When terminating Mosquitto the output is as expected:

    1598259315: mosquitto version 1.6.11 terminating
    1598259315: Saving in-memory database to /tmp/mosquitto.db.

    If i cat /tmp/mosquitto.db, I can see it contains my retained messages.

    When starting Mosquitto again, I expected to see that it read the database and that the retained messages I had published before terminating Mosquitto, would still be available (without having to re-publish them):

    mosquitto -c /mosquitto/config/mosquitto.conf
    1598259446: mosquitto version 1.6.11 starting
    1598259446: Config loaded from /mosquitto/config/mosquitto.conf.
    1598259446: Opening ipv4 listen socket on port 1883.
    1598259446: Opening ipv4 listen socket on port 8883.
    1598259446: Opening ipv6 listen socket on port 8883.
    1598259446: mosquitto version 1.6.11 running

    But as shown above, there is no indication that the database have been read (should it say something about that?) and my retained messages are gone.

    Do I need something else in my configuration, or have I misunderstood the persistence feature?

    Thanks
    Tue

    1. Hi
      I just tested it and it works. You might be reading the wrong .db file change the path in the conf file to a full file path.
      Rgds
      Steve

      1. Hi Steve
        Thanks a lot for your quick reply. I have tried several combinations of setting the filename and the path, also some that should not be valid according to the documentation, but nothing works. Should Mosquitto write something to stdout/stderr when starting that it is reading the database (like it does when writing the database)? Which persistence settings did you use?
        Can it be my publish that needs something before it is persisted. I have tried with the following publish:

        mosquitto_pub -h 127.0.0.1 -p 1883 -u xxx -P xxx -t test123 -m “test message” -q 2 -r

        Best regards

        Tue

        1. No it doesn’t tell you it is reading the database you do get an error that it is can’t write. Have you checked the timestamp on the file to check it is being updated?
          Also try without username/password as I didn’t check it with security.
          Other than that the settings are the same as you config file.
          I also just noticed that you are using a slightly newer version than I am. Are you on windows or Linux?
          Also I’m not sure that I use the persistence location I can’t check it at the moment try commenting that out
          Rgds
          Steve

          1. Hi Steve
            I have found the issue now. The problem is that mosquitto.db is written by the mosquitto user, but it seems like it reads it before it drops priveledges to the mosquitto user at startup. For this reason, the file can not be read unless I change the permissions before restarting mosquitto.
            -rw——- 1 mosquitt mosquitt 3095 Aug 26 06:57 /home/mos/mosquitto.db
            Thanks for your support!
            Tue

  2. Steve,
    I have been working with a setup using Node-red on a Pi3 with mosquitto as the broker. The client is a ESP32. This is a weather station I am trying to setup and used the N-R-Dashboard for the output. This all works fine and I have a nice display of the information that I can access on my Iphone(Safari), Pi3/4 (Chromium), IPad(Safari)(Chrome). All of these work well initially but after a day or so it starts giving me “lost connection” messages and is very intermittent then eventually it just stops providing any info. I am watching the Mosquitto log and I see message being received and sent, but nothing on the dashboard. Not sure were I should go from here. Been looking through your website but haven’t found the key to this problem.
    Thanks,
    Ken

    1. I’m assuming you are keeping the dashboard open all of the time. If you are then I would suspect a websockets timeout issue. If you close the browser and reconnect is it ok.
      Rgds
      Steve

      1. Steve,
        I just ran the experiment. Closed the Chromium browser on my Pi4. Re-Opened and then entered the address of the dashboard host etc. Continued to get a “Connection Lost” notification in the top bar of the Dashboard. The only time I get reliable Dashboard info is after reboot the Pi3 NR computer. Then it will report timely complete info for about a day and this on any of my other computers.
        thanks,
        Ken

        1. Hi
          Did you check that basic networking was working on the pi by doing a ping.If you start node-red manually from the command prompt you may also see something on the console that may help.
          Rgds
          Steve

          1. When I run a 10 ping sequence I get about 170 ms avg. response. but the dashboard never completely loads all the content but I can see all the message go thru mosquitto.

          2. Not sure of your question, “What dashboard are you using”. Dashboard from Node-red v1.1.2.
            This Node-red is running on a Pi3 and I view thru various browsers on Ipad, iphone, a pi4 using chromium etc.
            Thanks,
            Ken

          3. Hi
            Can you send me the flow you can post it in the comments or if it is one you downloaded from me tell me which one
            rgds
            Steve

  3. Hi
    I run Mosquitto on Pi with a connection from ESP32.
    I read data with ESP32 from Xiaomi temperature. Everything is fine, but after a while I lose contact with MQTT and I can’t publish anything. I checked and it doesn’t go into the “if (client.connected())” anymore, just in ” else { ”

    void publish(const char* addr, char* valueName, char* value) {
    char topic[50];
    sprintf(topic, “%s/%s/%s”, MQTT_PREFIX, addr, valueName);
    if (client.connected()) {
    Serial.print(“connected —— “);
    // client.publish(topic, value);
    client.loop();
    // Serial.print(“dupa loop – “);
    }
    else {
    Serial.println(“don’t connected—– “);
    // reconnect();
    // Serial.println(“Dupa reconectare – “);
    }

    Best regards
    Mihai

  4. Hi Steve, and thanks for your contributions to our understanding of MQTT!

    I’m trying to use mosquitto as a message-passing server. I want one part of the topic space (a/b/c/#) to be fully public (anybody can read/write.) And I want access to #SYS/ to be limited to the host the mosquitto server is running on. I’ll run a website there that subscribes to $SYS/broker to display current stats.

    I can’t figure out how to do this, either with a listener on a different port or with a user/password combo. In some cases mosquitto’s reporting errors on config file lines that don’t exist so I’m not sure what’s going on.)

    I’d expect this to be a common configuration but googling isn’t turning up anything helpful. Any suggestions? Or is there a better way to accomplish what I want?

    Thanks!

  5. Hi Steve,

    Thanks for your sharing the information.I have a question when using mosquitto and need your help.

    I have mosquitto broker running on the Ubuntu, use 6 embedded devices as the client for publishing information, and another client running on the Ubuntu subscribes to the information.However, some embedded devices will be disconnected after running for 1 week or 2 weeks, and the log infomation,
    1589849321: New connection from 192.168.1.114 on port 1883.
    1589849322: Socket error on client , disconnecting.

    The mosquitto.conf is as follows,
    message_size_limit 0
    port 1883
    max_connections -1
    require_certificate false
    persistence false

    I read a few posts you wrote and found that I did not use a CA certificate. Is this the problem? Can I use mosquitto normally without using this method?

    Regards,

    Andrew

    1. Can you just clarify what is happening.
      An embedded client disconnects and when it tries to reconnect it is rejected? is this what is happening.

      Is the client using a clean session? how long does the client remain disconnected?

      1. Hi Steven,
        Thanks for your response.
        After an embedded client works for a few days, there is a sudden disconnection and the broker cannot be connected again.It seems that mosquitto broker did not send CONNACK.
        The embedded client uses a clean session. Normally, after disconnecting, the client cannot connect to the server again. In this case, the device can only be powered off and restarted.

        Regards,

        Andrew

          1. Hi Steve,
            I am using paho.mqtt.embedded-c packet.I designed a state machine to handle connection, ping request and subscription information. And used a task of FreeRTOS to run this state machine.

            Thanks and regards,
            Andrew

  6. Hi Steve,

    Thanks for the quick guide.

    I have Mosquito broker and client installed on my raspberry pi, which I intend to create home automation solution.

    How many device maximum is best to be connected to the broker for efficiency, that is without having it to crash?

    Regards

  7. Hello Steve,

    In my system target, I want to use the mosquitto – client. Is this possible? Currently, I am not planning to use the mosquito broker. I downloaded the mosquito library with the broker and client and I can see a client in it.

    Thanks and regards – Dilip Jayavant

  8. I got the broker running on Ubuntu 18.04. Can use Pub and Sub and see messages. However, can’t make an MQTT connection on Windows machine on same local LAN subnet using MQTT Lens or MQTT Box. No WS or TLS so far.. Just basic connections. On that same Win machine connect using PuTTY to tcp port 1883. Any thoughts?

      1. Yes, I can connect to mqtt.eclipse.org using MQTT Box or Lens on a Windows machine. That is how I have been doing my testing.

        I’m able to connect to my internal broker ONLY through the Pub and Sub on the SAME linux box as the broker. I can, however, make a TCP connection to the broker linux box, port 1883 from the Windows machine. So I don’t believe it is a network routing issue.

        Thanks for your help.
        Tom

    1. Hi Steve,
      I am using Mosquitto running on Windows 7 machine. I can pub and sub using two different cmds on the same machine.
      I am trying to connect MKR1000 with this Mosquitto broker using its IP address but my pubsubClient.connect() fails with error state -2.

      Following your Post, I have configured mosqquitto with mosquitto-2.conf with:
      listener 1883
      allow_anonymous true
      max_packet_size 1280

      Any hint?

      Ehsan

  9. Hi Steve,
    I have requirement with mosqitto where one set of client would like to connect to mosquito with using TLS (certificate) but use passwords using port (1883)

    The other set of clients would like to connect with TLS and certificates. but using a different port (8883)

    Is this configuration possible ?

    If this is not possible can I run two copies of Mosquitto (with two different configuration file) listening on two ports ? as a service ?

  10. 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?

      1. Hi Steve,
        Thanks for the guidance, but I am not storing the messages directly, i am trying to parse the single message hex data recevied into mutilple decimal values. Since the incoming rate of messages is high, some of the data gets lost during parsing. So is there a way I can parse the data realtime and publish on webpages and store in db as well?

        1. Rose
          Did you try the script as the logging takes place in a separate thread. Do you have any idea of the messages/sec and I can try it.
          Rgds
          Steve

          1. Yes I am doing the parsing in a separate thread, and calling the data from the queue. Still the data seems to be lost.

            Around 6 messages 128 bytes long are being received in a second, each message is hex data and i’m parsing it into 7 decimal values and storing these 7 decimal values in db and sending over websocket.
            Will using node.js be a better option instead of python?

  11. 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

  12. 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

  13. 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 *