Connect with JavaScript (Node.js)
Use MQTT.js with the token authentication mode configured on a broker.
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 following environmental variables before running the example:
BROKER_URI
(e.g.mqtts://YOUR-BROKER.YOUR-NAMESPACE.cloudflarepubsub.com
)BROKER_TOKEN
with a valid auth tokenBROKER_TOPIC
to publish to - for example,hello/world
Before running the example, make sure to install the MQTT library:
Copy the following example as example.js
and run it with node example.js
.