Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
Why cannot I connect to hivemq broker the socket keep on getting closed when I try to reconnect?

I have wriiten to publish a rfid value and while subscribing inside javascript, the socket connection is lost and I have attached the screenshot of my console.

value.php

<body>

      <div id="print"></div>

      <!--  jquery library -->
      <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>

      <!--  paho MQTT library -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>

      <script src="app.js"></script>
</body>

enter image description here

I am trying to connect using hivemq broker and using port number of 8000 but it's not connecting.

app.js

var hostname = "broker.hivemq.com";
var clientId = "someid";
var username = "username";
var password = "password";
var subscription = "sometopicname";

mqttClient = new Paho.MQTT.Client(hostname, 8000, clientId);

mqttClient.onMessageArrived = MessageArrived;
mqttClient.onConnectionLost = ConnectionLost;
Connect();

function Connect() {
    mqttClient.connect({
        onSuccess: Connected,
        onFailure: ConnectionFailed,
        userName: username,
        password: password,
        useSSL: false
    });
}

function Connected() {
    console.log("Connected");
    mqttClient.subscribe(subscription);
}

function ConnectionFailed(res) {
    console.log("Connection failed: " + res.errorMessage);
}

function ConnectionLost(res) {
    if (res.errorCode !== 0) {
        console.log("Connection lost:" + res.errorMessage);
        Connect();
    }
}

function MessageArrived(message) {
        console.log(message.payloadString);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First, are you 100% sure that port 8000 is configured to support MQTT over Websockets.

Second, you appear to have a hard coded clientId value, with this you will only be able to have 1 client connected at a time. EVERY Page needs to have a unique clientId, if you try and connect 2 clients with the same clientId (2 or more instances of the page in any browser) will kick the other one off the broker. ClientIds MUST be globally unique.

If after you have checked the first and fixed the second you should check the broker logs to see why it may be closing the connection.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda