I found a lot of similar topics with the same issue, but after trying everything I found here and google, still I can’t connect to my cluster using a React app. The error is:
WebSocket connection to 'wss://b372c355b85c47dfa2d0e2633ec16662.s1.eu.hivemq.cloud:8884/mqtt' failed:
I am using wss:// and port 8884 with the sufix /mqtt
I checked if the connection is available using this command on mac: nc -zv b372c355b85c47dfa2d0e2633ec16662.s1.eu.hivemq.cloud 8884
what gives me:
Connection to b372c355b85c47dfa2d0e2633ec16662.s1.eu.hivemq.cloud port 8884 [tcp/*] succeeded!
I can connect using Node JS app I am using on the backend with the same configs (changing the user/pass and clientId only) from the same machine and network too. Can you help me to understand what I am missing?
I’m not very familiar with React code, but this improved example, based on your code, worked perfectly for me when connecting to my HiveMQ Cloud Serverless broker.
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
// For demonstration, remove StrictMode so that useEffect runs only once:
ReactDOM.createRoot(document.getElementById('root')).render(
// <React.StrictMode>
<App />
// </React.StrictMode>
);
Hey Diego, thanks for your answer, I have some new too. I think there is something more blocking the access from my machine with mac os, cause I tested it in another machine (running windows) and I had success without changing my code! What is curious is that from my mac I can use a similar approach from the backend system. Any ideias what can cause this kind of connection from a browser running on mac? (I tested using Google Chrome)
Several factors might be blocking or interfering with the MQTT over WebSocket connection on macOS compared to Windows.
macOS may have a firewall setting or a third-party antivirus suite filtering WebSocket traffic.
Some Chrome extensions, such as ad-blockers, privacy tools, or corporate security extensions, can block WebSocket connections. Try disabling extensions or using an Incognito window to test if this resolves the issue.
Start by isolating these potential factors to identify the cause. Once identified, configuring the settings appropriately should enable the MQTT over the WebSocket connection to work as expected.