Tiny MQTT Client (tinyMQTT)

Stripped out JavaScript MQTT module that does basic PUB/SUB, by @olliephillips. Espruino also contains a more complete MQTT module , but it uses substantially (3x) more memory.

  • Supports QoS 0 only.
  • Supports authentication on username and password.
  • 127 byte publishing length limit.

Some considerable effort has gone into ensuring safe reconnection in event of MQTT broker disconnecting us and or loss of network, minimising leaked memory and ensuring no duplicated event listeners, and subsequent processes.

How to use

Using the Espruino Web IDE you can just require the module directly:

var mqtt = require("tinyMQTT");

No config options

var mqtt = require("tinyMQTT").create(server);
mqtt.connect(); // Connects on default port of 1883

With config options

var mqtt = require("tinyMQTT").create(server, {
    username: "username",
    password: "password",
    port: 8883


var mqtt = require("tinyMQTT").create("test.mosquitto.org");

mqtt.on("connected", function(){

mqtt.on("message", function(msg){

mqtt.on("published", function(){
    console.log("message sent");

mqtt.on("disconnected", function(){

var wifi = require("Wifi");
wifi.connect("username", {password:"mypassword"}, function(){


If you want to reconnect in event of broker disconnection or wifi outage add mqtt.connect(); to the disconnected event listener. Reconnection will be attempted indefinitely, by default at 2 second intervals (though this can be configured). Once reconnected publishing should restart, and subscriptions will be honoured.

mqtt.on("disconnected", function(){


@gfwilliams, @tve, @HyGy, @MaBecker, @gulfaraz, @The-Futur1st and @wanglingsong. Thanks for the advice, tips, testing and pull requests!

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.