Skip to content
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

feat(ws): allow to use upgradeWebSocket in handler #3942

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

nakasyou
Copy link
Contributor

Closes #3005, and related to #3202 and #3845.

By the pull request, you would use upgradeWebSocket after return in handler like that:

const app = new Hono()

app.get('/', (c) => {
  return upgradeWebSocket(c, {
    onOpen: (_e, ws) => {
      ws.send('Hello World!')
    }
  })
})

A thing what I will be glad is c.req.param will be type-safe.

A code that shows c.req.param is completed by VSCode

Also bindings and variables are supported.

const app = new Hono<{
  Bindings: {
    DB: {
      insert: (v: string | Blob | ArrayBuffer) => void
    }
  }
}>()

app.get('/:room', (c) => {
  return upgradeWebSocket(c, {
    onMessage(e) {
      c.env.DB.insert(e.data)
    }
  })
})

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Feb 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.30%. Comparing base (a2ec848) to head (3265bb7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3942      +/-   ##
==========================================
- Coverage   91.32%   91.30%   -0.03%     
==========================================
  Files         168      168              
  Lines       10688    10786      +98     
  Branches     3070     3103      +33     
==========================================
+ Hits         9761     9848      +87     
- Misses        926      937      +11     
  Partials        1        1              

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

@yusukebe
Copy link
Member

Hey @nakasyou

This is a good idea! I like this approach. Please check the comment I left.

@nakasyou
Copy link
Contributor Author

Hi @yusukebe, I fixed the code from your review. Could you check it?

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.

Able to use upgradeWebSocket in handler
2 participants