1 Matching Annotations
  1. Jul 2023
    1. WebSocket support in Pub/Sub works by encapsulating MQTT packets (Pub/Sub’s underlying native protocol) within WebSocket framesExternal link icon

      ```js // Ensure MQTT.js is installed first // > npm install mqtt import * as mqtt from "mqtt"

      // Where 'url' is "mqtts://BROKER.NAMESPACE.cloudflarepubsub.com:8884" function example(url) { let client = mqtt.connect(url, { protocolVersion: 5, reconnectPeriod: 0, username: 'anything', password: jwt, // pass this from a form field in your app clientId: '', })

      client.on('connect', function () { client.subscribe(topic, function (err) { if (err) { client.end(); } else { console.log(subscribed to ${topic}) } })

      client.on('message', function (topic, message) { let line = (new Date()).toLocaleString('en-US') + ": " + message.toString() + "\n"; console.log(line) }) } ```