So i have a websocket server built with python, which is deployed in a EC2 container. Connecting through postman works perfectly, but when connecting via Websocket in React, it throws always this error:
WebSocket connection to ‘wss://xxx:3500/’ failed:
with no further description.
useEffect(() => {
const socket = new WebSocket(
process.env.NODE_ENV === 'production' ? "wss://xxxx:3500" : "ws://localhost:3500",
);
setSocket(socket);
socket.addEventListener("message", (event: MessageEvent<string>) => {
handleWebSocketMessage(event, setFutures, setStocks, setBonds, setDollars)
});
socket.addEventListener("error", (event) => {
console.error("WebSocket error:", event);
});
}, []);
The way i fixed this problem temporaly, was disabling the SSL certificates in my python websocket server, and connecting through ws instead of wss. But this is no solution, as i need it to be secure. The thing is that when the server is secure, it only works through postman.
I’ve already tryed debugging by connecting to a mock wss server in my React websocket connection and it worked perfectly, so i made the conclution that the client isn’t the problem. What could be happening?
2
Answers
I fixed the problem by fixing my websocket server certificates. The reason it was failing on my browser and not on postman was because postman doesn't care about the server certificate, while an https client, can only connect to a wss server.
Are you using nginx? Because if you are, you should add something like this to your .conf file: