Websocket does not work in production with CF, but works fine locally

What is the name of the domain?

https://between-orders.victormvieira99.workers.dev

What is the issue you’re encountering

I need to use a websocket connection for a client that needs to update its orders in real time, and after several tests on local it works perfectly, however, when deploying the Cloudflare Worker, all the HTTP requests work, but the WSS does not work. It returns a 404 error and I can’t get the problem.

What are the steps to reproduce the issue?

app.get(‘/ws’, async (c) => {
if (c.req.header(“Upgrade”) !== “websocket”) {
return c.text(“Expected Upgrade: websocket”, 426);
}

const id = c.env.WEBSOCKET_ROOM.idFromName(‘orders-room’);
const stub = c.env.WEBSOCKET_ROOM.get(id);

broadcast(c, await broadcastOrchestrator(c.env.DB));

const response = await stub.fetch(c.req.raw);
return new Response(response.body, response);
});

export async function broadcast(c: any, order: any){
const id = c.env.WEBSOCKET_ROOM.idFromName(‘orders-room’);
const stub = c.env.WEBSOCKET_ROOM.get(id);

await stub.broadcast(JSON.stringify(order));
}

export default app

export class WebSocketRoom extends DurableObject {
connections: Set;

constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.connections = new Set();
}

async fetch(req: Request) {
try {
const webSocketPair = new WebSocketPair();
const [client, server] = Object.values(webSocketPair);

  server.accept(); 
  this.connections.add(server); 

  server.addEventListener("message", (event) => {
    console.log("Mensaje recibido:", event.data);
    this.broadcast(`Broadcast: ${event.data}`);
  });

  server.addEventListener("close", () => {
    this.connections.delete(server);
    console.log("Cliente desconectado.");
  });

  return new Response(null, { status: 101, webSocket: client });
} catch (error) {
  console.error("Error manejando la conexión WebSocket:", error);
  return new Response("Error interno del servidor", { status: 500 });
}

}

webSocketError(ws: WebSocket, error: unknown) {
this.connections.delete(ws);
}

webSocketClose(
ws: WebSocket,
_code: number,
_reason: string,
_wasClean: boolean
) {
this.connections.delete(ws);
}

async broadcast(message: string) {
for (const connection of this.connections) {
connection.send(message);
}
}
}

Screenshot of the error

I saw the same error in another publication, but I saw that they could not give you an answer, this was the publication:

@alexander7

Hmm, want to go HTTPS and use WS? Might want to go with WSS instead :thinking:

Are WebSockets enabled at Cloudflare dashboard?

Ou well, that’s strange then.
Was your Website working and WebSockets over a HTTPS fine before adding Cloudflare on top? :thinking:

May I ask what SSL option have you got selected under the SSL/TLS tab at Cloudflare dashboard for your domain ( Flexible, Full, Full Strict … )? :thinking:

Wonder how connection is kept, or it closes? Do you get ping-pong or even checking it later anymore?

Do you run any proxy between for this such as Nginx?

Did you tried any of the suggestions already from those posts in the meantime? :thinking:

This topic was automatically closed after 15 days. New replies are no longer allowed.