MQTT.js is a client library for the MQTT protocol in JavaScript. It targets Node.js and browser environments and can connect to brokers over TCP, TLS, and WebSockets.
Overview
MQTT.js implements an MQTT client you embed in Node.js or browser applications. It speaks to brokers using protocols such as mqtt, mqtts, ws, wss, and related variants, and exposes connect, publish, subscribe, and unsubscribe APIs plus synchronous async wrappers.
The client wraps connections over arbitrary transports, handles server keepalives, QoS flows, and optional automatic reconnection. You can tune reconnect timing, refresh WebSocket URLs on reconnect via hooks, and customize WebSocket creation for proxies or extra headers.
MQTT 5 features include topic alias handling, session properties, custom acknowledgment handling, and stores for incoming and outgoing packets. TypeScript types are part of the codebase, and the package can run in React Native with the documented setup.
A bundled CLI can publish and subscribe when installed globally. Documentation also points to external brokers such as Mosquitto or Aedes when you need your own server.
Key features
- MQTT client for Node.js and browsers
- TCP, TLS, and WebSocket transports
- Automatic reconnection with tunable options
- MQTT 5 properties and topic alias support
- Async connect with Promise-based API
- Bundled mqtt CLI for broker interaction
- TypeScript-oriented client implementation
- React Native usage with example guidance
Use cases
- IoT device telemetry to an MQTT broker
- Browser apps over WebSocket MQTT
- Pub/sub messaging in Node.js services
- Testing brokers from the command line