WebSocket Not Connecting Over HTTPS (wss://) but Works on http://localhost #5301
Unanswered
DivyaChinthala
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hi! Which platform are you using for your production environment? Does it support WebSocket connections? Reference: https://socket.io/docs/v4/reverse-proxy/ |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone,
I'm working on a Next.js (Frontend) and Node.js (Backend) application using Socket.io for real-time updates. My WebSocket connection works fine on http://localhost, but when deployed to a production environment (https://), the connection fails.
Frontend Code
import { io } from "socket.io-client";
const socket = io("https://************.amazonaws.com", {
transports: ["websocket"],
reconnectionAttempts: 3,
});
socket.emit("join", { userId, brandId, role: roleId });
socket.on("connect", () => {
console.log("✅ Connected to WebSocket");
});
socket.on("connect_error", (error) => {
console.error("❌ WebSocket connection error:", error);
});
Backend Code
import http from "http";
import { Server, Socket } from "socket.io";
const ServerInstance: http.Server = http.createServer();
setupSocket(ServerInstance);
function setupSocket(server: http.Server) {
const io = new Server(server, {
cors: { origin: "*" }, // Allowing all origins
});
io.on("connection", (socket: Socket) => {
console.log(
✅ User connected: ${socket.id}
);});
}
Issue:
WebSocket connects successfully on http://localhost
WebSocket fails when using https://my-websocket-server.com
Error when using npx wscat -c wss://my-websocket-server.com
Unexpected server response: 403
Beta Was this translation helpful? Give feedback.
All reactions