Skip to content

Conversation

Copy link

Copilot AI commented Sep 6, 2025

Issue

WebSocket connections were failing with 400 Bad Request errors when using websocat or other WebSocket clients:

❯ websocat ws://127.0.0.1:3000/ws?hash=c39ecb75e8b3f5aac139d775e215bb0a
websocat: WebSocketError: WebSocketError: Received unexpected status code (400 Bad Request)
websocat: error running

Root Cause

The WebSocket implementation was using @hono/node-ws with sub-routing (app.route("/ws", wsRoute)), which prevented the WebSocket upgrade process from working correctly. The WebSocket setup was isolated in a separate route module that couldn't properly inject WebSocket support into the main server.

Solution

Moved the WebSocket configuration from sub-routes to the main application:

  • Before: WebSocket setup was in server/routes/ws.ts using createNodeWebSocket({ app }) on a sub-app
  • After: WebSocket setup is in server/main.ts using createNodeWebSocket({ app }) on the main app
  • Preserved all existing middleware logic (validateHash) and WebSocket handlers
  • Cleaned up unused route definitions while maintaining the validation function

Technical Details

The key changes:

  1. Moved WebSocket setup to main app: createNodeWebSocket({ app }) now operates on the primary Hono instance
  2. Integrated middleware directly: validateHash middleware works correctly with WebSocket upgrade
  3. Proper error handling: Invalid hashes now return appropriate responses (though displayed as 400 during WebSocket upgrade due to library behavior)

Verification

WebSocket connections now work correctly:

# Before: Failed with 400 Bad Request
# After: Connects and properly validates, rejects invalid hashes as expected
websocat ws://127.0.0.1:3000/ws?hash=valid_hash

Hash validation works:

  • Valid hashes: WebSocket connection succeeds
  • Invalid hashes: Connection rejected with error (correct behavior)
  • HTTP requests: Return proper 404 "problem not found" for invalid hashes

Backward compatibility maintained:

  • All existing API endpoints continue to work
  • Problem loading and management unchanged
  • WebSocket message handling logic preserved

Impact

This fixes the fundamental WebSocket connectivity issue. Users can now successfully connect to WebSocket endpoints using websocat, browser WebSocket APIs, or other WebSocket clients when providing valid problem hashes.

Created from VS Code via the GitHub Pull Request extension.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Sep 6, 2025
@syoch syoch closed this Sep 6, 2025
@syoch syoch deleted the copilot/vscode1757149048732 branch September 6, 2025 09:02
Copilot AI changed the title [WIP] WebSocket Connection Issue Fix WebSocket connection failure due to incorrect sub-routing setup Sep 6, 2025
Copilot AI requested a review from syoch September 6, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants