Cloudflare Docs
Visit PubSub on GitHub
Set theme to dark (⇧+D)

Connect with JavaScript

Below is an example using MQTT.js with the TOKEN authentication mode configured on a broker. The example assumes you have Node.js v16 or higher installed on your system.

Make sure to set the BROKER_URI (e.g. mqtts://, BROKER_TOKEN (a valid auth token), and BROKER_TOPIC environmental variables before running the example program.

# Pre-requisite: install MQTT.js
npm install mqtt --save

Copy the following example as example.js and run it with node example.js.

const mqtt = require("mqtt");
// Specify MQTT broker URI: mqtts://<broker name>.<namespace>
const uri = check_env(process.env.BROKER_URI);
// Any username and your token from the /brokers/YOUR_BROKER/credentials endpoint
// The token should be the base64-encoded JWT issued by the Pub/Sub API
const username = "anything";
const password = check_env(process.env.BROKER_TOKEN);
// Specify a topic name to subscribe to and publish on
let topic = check_env(process.env.BROKER_TOPIC);
// Configure and create the MQTT client
const client = mqtt.connect(uri, {
protocolVersion: 5,
port: 8883,
clean: true,
connectTimeout: 2000, // 2 seconds
clientId: "",
// Emit errors and exit
client.on("error", function (err) {
console.log(`⚠️ error: ${err}`);
// Connect to your broker
client.on("connect", function () {
console.log(`🌎 connected to ${process.env.BROKER_URI}!`);
// Subscribe to a topic
client.subscribe(topic, function (err) {
if (!err) {
console.log(`✅ subscribed to ${topic}`);
// Publish a message!
client.publish(topic, "My first MQTT message");
// Start waiting for messages
client.on("message", async function (topic, message) {
console.log(`received a message: ${message.toString()}`);
// Goodbye!
// Return variable or throw error
function check_env(env) {
if (!env) {
throw "BROKER_URI, BROKER_TOKEN and BROKER_TOPIC must be set.";
return env;