How to connect firebase hosting with wss mosquitto?

62 Views Asked by At

I want to connect hosting firebase with wss mosquitto. Domain is https://tkllhk231.web.app/. I configured mosquitto.conf. Code is running in edge : https://tkllhk231.web.app/.

listener 8084
protocol websockets
allow_anonymous true
cafile C:/Program Files/mosquitto/Firebase/public/certificates/ca.crt
certfile C:/Program Files/mosquitto/Firebase/public/certificates/server.crt
keyfile C:/Program Files/mosquitto/Firebase/public/certificates/server.key
tls_version tlsv1

I've successfully connected using MQTTX over WSS, but I'm encountering issues connecting to Firebase hosting. It my code:

function connectToBroker() {
  const clientId = "client" + Date.now();
  const host = "wss://172.20.107.226:8084/ws";
  
  const caPath = "/certificates/ca.crt";
  const certPath = "/certificates/server.crt";
  const keyPath = "/certificates/servver.key";

  // Fetch CA certificate
  fetch(caPath)
    .then(response => response.text())
    .then(ca => {
      // Fetch client certificate
      fetch(certPath)
        .then(response => response.text())
        .then(cert => {
          // Fetch private key
          fetch(keyPath)
            .then(response => response.text())
            .then(key => {
              const options = {
                keepalive: 60,
                clientId: clientId,
                protocolId: "MQTT",
                protocolVersion: 4,
                clean: true,
                reconnectPeriod: 1000,
                connectTimeout: 30 * 1000,
                ca: ca,
                cert: cert,
                key: key,
              };

              const mqttClient = mqtt.connect(host, options);
              mqttClient.on("error", (err) => {
                console.log("Error: ", err);
                mqttClient.end();
              });

              mqttClient.on("reconnect", () => {
                console.log("Reconnecting...");
              });

              mqttClient.on('connect', function () {
                console.log('Connect Successfully broker');

I connect successfully

I have tried using MQTTX to connect to WSS, and it was successful. However, when attempting to use Firebase hosting, it is reconnect.

0

There are 0 best solutions below