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: 12   Average: 4.5/5]

38 comments

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

  2. 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!

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

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

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

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

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

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

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

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

  11. 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 to steve Cancel reply

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