Cloudflare 中文文档
PubSub
编辑这个页面
跳转官方原文档
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 following environmental variables before running the example:

  1. BROKER_URI (e.g. mqtts://YOUR-BROKER.YOUR-NAMESPACE.cloudflarepubsub.com)
  2. BROKER_TOKEN with a valid auth token
  3. BROKER_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.