Skip to content

Conversation

@poteboy
Copy link

@poteboy poteboy commented Dec 9, 2025

Summary

Fix WebSocket connections failing when the endpoint is registered under app.route().

Problem

When using app.route(), WebSocket connections fail with 400 Bad Request.

// ❌ This doesn't work
const subApp = new Hono()
subApp.get('/ws', upgradeWebSocket(() => ({ ... })))
app.route('/api', subApp)

// ✅ This works
app.get('/ws', upgradeWebSocket(() => ({ ... })))

Cause

The original implementation stored a connection symbol in c.env:

c.env[CONNECTION_SYMBOL_KEY] = connectionSymbol

However, this doesn't persist when the WebSocket endpoint is registered via app.route(). The exact reason is unclear, but it seems c.env reference changes in that case.

Solution

Instead of writing to c.env, use a WeakSet to track the request object directly. Since c.env.incoming (the request object) remains the same reference even through app.route(), this approach works reliably.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • yarn changeset at the top of this repo and push the changeset
  • Follow the contribution guide

@changeset-bot
Copy link

changeset-bot bot commented Dec 9, 2025

🦋 Changeset detected

Latest commit: d90a14d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/node-ws Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov
Copy link

codecov bot commented Dec 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.41%. Comparing base (eea3e9b) to head (d90a14d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1623   +/-   ##
=======================================
  Coverage   84.41%   84.41%           
=======================================
  Files         120      120           
  Lines        3894     3895    +1     
  Branches     1052     1052           
=======================================
+ Hits         3287     3288    +1     
  Misses        516      516           
  Partials       91       91           
Flag Coverage Δ
node-ws 1.85% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
Copy link
Member

yusukebe commented Dec 9, 2025

@poteboy Thank you for the PR!

Hey @nakasyou , can you review this? No rush.

@nakasyou
Copy link
Contributor

nakasyou commented Dec 9, 2025

Hi @poteboy thank you for fixing a problem!

Hey @yusukebe, I have no time to review now, but I can review it after Saturday.

Copy link
Contributor

@nakasyou nakasyou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late reply. This is great idea to solve it, thank you.

Comment on lines 27 to 30
const generateConnectionSymbol = () => Symbol('connection')

/** @example `c.env[CONNECTION_SYMBOL_KEY]` */
const CONNECTION_SYMBOL_KEY: unique symbol = Symbol('CONNECTION_SYMBOL_KEY')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be deleted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@poteboy Can you handle this?

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.

3 participants