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 following environmental variables before running the example:
BROKER_URI(e.g.mqtts://YOUR-BROKER.YOUR-NAMESPACE.cloudflarepubsub.com)BROKER_TOKENwith a valid auth tokenBROKER_TOPICto 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.