How to Install and Run The Mosca MQTT Broker on Node-Red

node-red-mosca-iconRather than use an external broker like mosquitto with node-red you can install Mosca which is a MQTT broker written in node.js.

Because Mosca isn’t part of the core nodes you need to install it using the npm package manager or through the Node-red Admin control panel.

Mosca Install Using Node-red Admin

In the node-red admin go to Settings and manage Pallette.


Click on the install tab and search for node-red-contrib-mqtt.

You should see a list of options. The node-red-contrib-mqtt-broker is the module you need to install.Install-mosca-broker


After the install you should find a new node called mosca under the input categories.

Drag and drop on the Canvas to use.

Mosca Node Configuration

The node properties is a single page and with very few settings.mosca-node-properties

You need to set the ports for standard MQTT and MQTT over websockets.

The username and password fields are for the mongodb database,and you can usually leave them blank even when using the database.

The mongo database is used for persistent data and is usually as show in the screenshot.


Note: If it appears grayed out then it is not being used.

Persistent data stores unsent messages,subscribes and retained messages so they are remembered if the server is restarted.

To use it you will need to have the mongodb server installed on your system.


Important Note: This isn’t the mongodb node. The node is a connector to a database server and not the server itself.

You will need to install the mongodb database server separately it is not a node-red node.

Testing The Install

You can use any MQTT client for testing the broker.The easiest is to use the MQTT input and output nodes.

Here is a screen shot of my test flow:


Here is the flow code which you can import into your own flow:

[{"id":"72147d2d.44f4d4","type":"debug","z":"eb695aa9.0527e","name":"","active":true,"console":"false","complete":"false","x":331,"y":164,"wires":[]},{"id":"91f5338c.38bbc8","type":"mosca in","z":"eb695aa9.0527e","mqtt_port":"1884","mqtt_ws_port":8080,"name":"","username":"steve","password":"password","dburl":"","x":149,"y":105,"wires":[[]]},{"id":"1a2ed5c8.b06cf2","type":"mqtt in","z":"eb695aa9.0527e","name":"","topic":"sensors/moscatest","qos":"0","broker":"c6953943.ef9508","x":132,"y":165,"wires":[["72147d2d.44f4d4"]]},{"id":"87156fac.5e8bb","type":"mqtt out","z":"eb695aa9.0527e","name":"","topic":"","qos":"","retain":"","broker":"c6953943.ef9508","x":331,"y":230,"wires":[]},{"id":"d7f781ca.ab16e","type":"inject","z":"eb695aa9.0527e","name":"send message","topic":"sensors/moscatest","payload":"test message","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":145,"y":230,"wires":[["87156fac.5e8bb"]]},{"id":"c6953943.ef9508","type":"mqtt-broker","z":"","name":"Mosca","broker":"localhost","port":"1884","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

Video- How to Install The Mosca MQTT Broker on Node-Red


The Node-Red mosca node is useful for testing it may also be useful for small home automation networks.

It is not really suitable for a production environment.

I will be watching to see what happens with this node going forward.

Related Tutorials and Resources

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


  1. There is a new MQTT broker node for Node-Red called node-red-contrib-aedes which uses aedes.js under the hood.
    Node-red-contrib-mqtt-broker was based on mosca.js which is not supported anymore. The Mosca team decided to stop the develoment and created Aedes instead.
    Have a look and let me know if it works for you.

  2. Hi, I managed to install the broker correctly thanks to this tutorial and made the test with the debug node. My only doubt is, how can I make this work with a device connected on a different network? Which address do I have to use on that case?

    1. You use the IP address of the machine running node red.Type ipconfig (windows) or ifconfig (linux) at the command line for the ip address

  3. Hi, I want to send the data of Temperature sensor from ESP8266 to node red. I am using Mosca MQTT broker in node red and using windows operating system make this thing work but due to some issue, I am not able to send the data from esp8266 to Mosca MQTT broker in Node-Red.
    Any suggestion on this will be a great help.

      1. Firstly I would use the aedes broker. It replaces the mosca broker and is being updated.
        All you then need to do is to enter the broker address into the sensors and send data. You will then need a client to subscribe and read the data. You can use node-red MQTT node to do that

          1. Martin
            Sorry I don’t use those sensors so couldn’t really say but they must have some kind of configuration file.
            Can you send me the code your are using to read the values. Use the ask-steve page to send it

  4. Hello everyone!
    I made all the steps, everything is ok.
    I install the mongodb server (2.4.10), but i need help to setup the mongodb database server
    and connect to my broker.
    I don’t know how to do this, if anyone can help me!!!

    Thank you.


  5. Steve
    I have tried using Mosca on Node Red but the system crashes.
    I get an error: Uncaught exception Type error;
    “cannot set property listening or # server which has only a getter”
    then it enters a “failed state” and goes into a rebooting loop.
    I lost the entire program and had to start fresh install.
    Have you heard of any issues with Mosca?

    1. Thats what I thought but when I tested it it didn’t work that way. It did work on the mongodb database fro remote access.

Leave a Reply

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