Skip to content

WebSocket upgrade fails with 200 OK response after successful beforeHandle #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
patrik-u opened this issue Apr 19, 2025 · 1 comment · May be fixed by #1187
Open

WebSocket upgrade fails with 200 OK response after successful beforeHandle #1169

patrik-u opened this issue Apr 19, 2025 · 1 comment · May be fixed by #1187
Labels
bug Something isn't working

Comments

@patrik-u
Copy link

What version of Elysia is running?

1.2.25

What platform is your computer?

Microsoft Windows NT 10.0.19045.0 x64

What steps can reproduce the bug?

  1. Start minimal ElysiaJS WS:
import Elysia from "elysia";

export const app = new Elysia()
    .ws("/ws", {
        // NO schemas (query, body) defined here
        beforeHandle({ request }) {
            const url = new URL(request.url);
            console.log(`Ultra-Minimal WS: beforeHandle. Path: ${url.pathname}`);
            // Just return a dummy context synchronously
            return { userId: "ultra-minimal-user" };
        },
        open(ws) {
            console.log(`Ultra-Minimal WS: Opened! Context: ${JSON.stringify(ws.data)}`);
            ws.send("Ultra-Minimal Connected!");
        },
        message(ws, message) {
            console.log(`Ultra-Minimal WS: Message: ${message}`);
            ws.send(`Echo: ${message}`);
        },
        close(ws, code, reason) {
            console.log(`Ultra-Minimal WS: Closed. Code: ${code}, Reason: ${reason}`);
        },
    })
    .get("/", () => "Hello")
    .listen(3000, () => {
        console.log("Ultra-Minimal Elysia server running on port 3000");
    });
  1. Run: wscat -c "ws://127.0.0.1:3000/ws"

  2. Observe Unexpected server response: 200.

  3. Run minimal_ws_test.ts.

// minimal_ws_test.ts
import { type WebSocketHandler } from "bun";

console.log("Starting minimal Bun WebSocket server...");

const server = Bun.serve({
    port: 3001, // Use a different port
    fetch(req, server) {
        const url = new URL(req.url);
        if (url.pathname === "/ws_minimal") {
            console.log("Minimal WS: Received upgrade request");
            // Simulate basic auth check - just check if token exists
            const token = url.searchParams.get("token");
            if (!token) {
                console.log("Minimal WS: No token, rejecting.");
                return new Response("Auth token required", { status: 401 });
            }

            console.log("Minimal WS: Token found, attempting upgrade...");
            const success = server.upgrade(req, {
                data: { authToken: token }, // Pass data like Elysia context
            });

            if (success) {
                console.log("Minimal WS: Upgrade call successful.");
                // Bun automatically handles sending the 101 response here
                return undefined;
            } else {
                console.log("Minimal WS: Upgrade call failed.");
                return new Response("Upgrade failed", { status: 500 });
            }
        }
        return new Response("Not Found", { status: 404 });
    },
    websocket: {
        open(ws) {
            console.log(`Minimal WS: Socket opened! Data: ${JSON.stringify(ws.data)}`);
        },
        message(ws, message) {
            console.log(`Minimal WS: Received message "${message}" from ${ws.data.authToken}`);
            ws.send(`Echo: ${message}`);
        },
        close(ws, code, reason) {
            console.log(`Minimal WS: Socket closed. Code: ${code}, Reason: ${reason}`);
        },
    } satisfies WebSocketHandler<{ authToken: string }>,
});

console.log(`Minimal Bun WebSocket server listening on port ${server.port}`);
  1. Run wscat -c ws://127.0.0.1:3001/ws_minimal?token=test.

  2. Observe successful connection.

What is the expected behavior?

Successful connection after running: wscat -c "ws://127.0.0.1:3000/ws"

What do you see instead?

I see Unexpected server response: 200.

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

Yes.

@patrik-u patrik-u added the bug Something isn't working label Apr 19, 2025
@carlosmfreitas2409
Copy link

beforeHandle is also not executing on my side.
Elysia v1.3.0
Bun v1.2.4

@carlosmfreitas2409 carlosmfreitas2409 linked a pull request May 6, 2025 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants