This is an implementation of the MQTT 3.1.1 protocol written in TypeScript.
It is not a port of the excellent MQTT.js package. I wrote it for "fun", originally using Flow, but never finished and then forgot about it. When I saw there were no MQTT modules for Deno, I decided to convert it to TypeScript as an exercise in learning Deno.
Since the core of the library has no dependencies, it wasn't too difficult to add support for Node.js and browsers so why not?
import { Client } from 'https://deno.land/x/mqtt/deno/mod.ts'; // Deno (ESM)
// const { Client } = require('@jdiamond/mqtt'); // Node.js (CommonJS)
// import { Client } from 'https://unpkg.com/@jdiamond/mqtt-browser'; // Browsers (ESM)
const client = new Client({ url: 'mqtt://test.mosquitto.org' }); // Deno and Node.js
// const client = new Client({ url: 'ws://test.mosquitto.org:8081' }); // Browsers
await client.connect();
await client.subscribe('incoming/#');
client.on('message', (topic, payload) => {
console.log(topic, payload);
});
await client.publish('my/topic', 'my payload');
await client.disconnect();
See the API documentation for more details.
The "raw" TypeScript files are import'able by Deno.
The Deno Client
uses Deno.connect
to create TCP connections so --allow-net
is required when running code using this module.
Look in examples/deno to see examples of using the client.
There are some CLI tools in tools that are similar to mosquitto_pub and mosquitto_sub.
To subscribe:
deno run --allow-net tools/sub.ts -u mqtt://test.mosquitto.org -t "MQTT.ts/test/topic" -v
To publish:
deno run --allow-net tools/pub.ts -u mqtt://test.mosquitto.org -t "MQTT.ts/test/topic" -m "hello"
The Node.js Client
uses the net
module to create TCP connections.
This build is published to npm as @jdiamond/mqtt and can be imported like any normal npm package.
Examples in examples/node.
The browser Client
uses a WebSocket
object to connect to a broker that supports MQTT over WebSockets.
This build is published to npm as @jdiamond/mqtt-browser and available via unpkg.com here:
https://unpkg.com/@jdiamond/mqtt-browser
The UMD build for older browsers is available here:
https://unpkg.com/browse/@jdiamond/mqtt-browser/index.min.js
Example in examples/browser.
First started working with Deno 1.0.0, but I only test with recent versions (most recently 1.24.1). Maybe I should set up some GitHub actions?
To lint, check types, and run tests:
deno lint
deno task tsc
deno test
# Or, run all with:
deno task check
To run a local broker on macOS:
brew install mosquitto
/usr/local/sbin/mosquitto -c mosquitto-mac.conf
To run a local broker on Ubuntu in WSL2:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt update
sudo apt install mosquitto
mosquitto -c mosquitto-wsl2-ubuntu.conf
To test publishing and subscribing to your local broker, run these commands in separate shells:
deno run --allow-net tools/sub.ts -t "foo/#" -v
deno run --allow-net tools/pub.ts -t "foo/bar" -m "baz"
To make a release:
deno task check
cd browser
# update version in package.json
npm install
npm run build
npm publish
cd ..
cd node
# update version in package.json
npm install
npm run build
npm publish
cd ..
git tag x.y.z
git push --tags
- 5.0: https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html
- 3.1.1: https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
- 3.1: https://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
- finish API docs
- protocol version 3.1
- mqtts for deno and node clients
- use native event target/emitter classes
- events for messages matching topic filters
- async iterators for messages matching topic filters
- make disconnect wait until all publishes sent/acknowledged
- address all TODO comments in code
- release process
- tag for deno.land/x to use
- publish Node.js and browser builds to npm
- keep in sync or allow versions to drift?
- protocol version 5.0
- round robin connect to multiple brokers
- benchmarking and performance improvements
- MQTT over QUIC
- base class for server applications?