explaingit

mqttjs/mqtt.js

9,066TypeScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

MQTT.js is a JavaScript library for sending and receiving messages between connected devices using the MQTT protocol, the standard for smart home and IoT communication, in both Node.js and the browser.

Mindmap

mindmap
  root((mqtt.js))
    What it does
      MQTT messaging
      Publish subscribe
      Auto reconnect
    Where it runs
      Node.js
      Browser
      Command line
    Protocol support
      MQTT 3.1
      MQTT 3.1.1
      MQTT 5.0
    Use cases
      Smart home
      IoT sensors
      Real-time data
    Features
      QoS levels
      Auth support
      TypeScript types
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Connect a Node.js app to an MQTT broker to receive real-time temperature or sensor readings from smart home devices.

USE CASE 2

Build a browser-based dashboard that shows live updates from IoT devices over a WebSocket MQTT connection.

USE CASE 3

Test MQTT topics from the command line using the built-in CLI tool without writing any code.

USE CASE 4

Publish control commands from a web app to smart home devices like lights or thermostats via an MQTT broker.

Tech stack

TypeScriptJavaScriptNode.jsWebSocket

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

MQTT.js is a JavaScript library that lets Node.js applications and web browsers communicate using the MQTT protocol. MQTT is a lightweight messaging standard designed for situations where devices need to send small amounts of data reliably over potentially unreliable connections, which is why it is common in smart home devices, industrial sensors, and other connected hardware. The way MQTT works is through a publish-subscribe model. Instead of two devices talking directly to each other, they both connect to a central server called a broker. One device publishes a message to a named channel, called a topic, and any other device that has subscribed to that topic receives the message. MQTT.js handles all the client-side mechanics of this: connecting to the broker, sending messages, receiving messages, and reconnecting automatically if the connection drops. For developers, the library is installed through npm and works in Node.js or in the browser via WebSocket connections. It includes a command-line tool so you can publish and subscribe to topics directly from a terminal without writing any code, which is useful for testing. The library supports MQTT versions 3.1, 3.1.1, and 5.0, with the latter adding features like message expiry and better error reporting. The current major version (v5) was rewritten in TypeScript, which provides type definitions that help catch mistakes at development time in typed projects. The library handles reconnection logic, optional message delivery guarantees (called QoS levels), and authentication, including a hook for refreshing expired credentials when a connection needs to re-establish. MQTT.js is the most widely used MQTT client in the JavaScript ecosystem and is maintained as an open source project. The full README is longer than what was shown.

Copy-paste prompts

Prompt 1
I have a Mosquitto MQTT broker running locally and want to subscribe to a sensor topic in Node.js using mqtt.js. Show me the minimal working code.
Prompt 2
How do I use mqtt.js in a React app to receive live IoT sensor updates over WebSocket?
Prompt 3
I want to publish a message to an MQTT topic from the command line to test my broker. What mqtt.js CLI command do I run?
Prompt 4
How do I handle automatic reconnection in mqtt.js so messages are not lost when the broker goes offline briefly?
Prompt 5
Show me how to add credential-refresh authentication in mqtt.js v5 when reconnecting after a token expires.
Open on GitHub → Explain another repo

← mqttjs on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.