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

Able to use upgradeWebSocket in handler #3005

Open
nakasyou opened this issue Jun 20, 2024 · 4 comments · May be fixed by #3942
Open

Able to use upgradeWebSocket in handler #3005

nakasyou opened this issue Jun 20, 2024 · 4 comments · May be fixed by #3942
Labels
enhancement New feature or request.

Comments

@nakasyou
Copy link
Contributor

nakasyou commented Jun 20, 2024

What is the feature you are proposing?

I think accepting using upgradeWebSocket like a stream is good.
For example:

import { Hono } from 'hono'
import { upgradeWebSocket } from '.....'

const app = new Hono()

app.get('/ws', c => {
  return upgradeWebSocket(c, {
    onOpen () {
      console.log('opened!')
    }
  })
})

Because user can write more flexible code such as creating custom response in WebSocket route.

@nakasyou nakasyou added the enhancement New feature or request. label Jun 20, 2024
@Lucifer359-IV
Copy link

but why a right minded person will create custom response on this kind of thing

@nakasyou
Copy link
Contributor Author

Hi @Lucifer359-IV, for example, you can do that

app.get('/ws', c => {
  if (!validToken(c.req.query('token'))) {
    return c.text('Unauthorized', 400)
  }
  return upgradeWebSocket(c, {
    onOpen () {
      console.log('opened!')
    }
  })
})

Also you can change the response with or without the upgrade header.

@JoaquimLey
Copy link
Contributor

@nakasyou can't you do that token validation with a middleware?

Also have you checked the streaming helper section in the docs?

@nakasyou
Copy link
Contributor Author

nakasyou commented Oct 20, 2024

@JakeChampion

can't you do that token validation with a middleware?

Yes, but it expand the options.
Also, currently, c.req.param is not typed.

app.get('/:id', upgradeWebSocket((c) => {
  c.req.param('id') // not typed
}))

To implement this, the problem will be resolved.

Also have you checked the streaming helper section in the docs?

Yes. I used "like a stream" in the sense of generating a response, not as a middleware, but that expression may not be appropriate. I will change the title. Thanks.

@nakasyou nakasyou changed the title Able to use upgradeWebSocket like a stream Able to use upgradeWebSocket in handler Oct 20, 2024
@nakasyou nakasyou linked a pull request Feb 22, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants