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

Error using WebSocket Helper for Cloudflare Workers #2883

Closed
ronkeiser opened this issue Jun 1, 2024 · 6 comments
Closed

Error using WebSocket Helper for Cloudflare Workers #2883

ronkeiser opened this issue Jun 1, 2024 · 6 comments
Labels

Comments

@ronkeiser
Copy link
Contributor

What version of Hono are you using?

4.4.2

What runtime/platform is your app running on?

Cloudflare Workers

What steps can reproduce the bug?

Range Error

Employing the upgradeWebSocket helper produces an error and immediately returns a 500 response:

✘ [ERROR] RangeError: Responses with a WebSocket must have status code 101.

Minimal Example

Here is a minimal example using a snippet taken from the docs:

import { Hono } from 'hono'
import { upgradeWebSocket } from 'hono/cloudflare-workers'

const app = new Hono()

app.get(
	'/ws',
	upgradeWebSocket((c) => {
		return {
			onMessage(event, ws) {
				console.log(`Message from client: ${event.data}`)
				ws.send('Hello from server!')
			},
			onClose: () => {
				console.log('Connection closed')
			},
		}
	}),
)

export default app

Example Repo

I also created a repo with the example so that this can be easily reproduced:
https://github.com/ronkeiser/hono-ws-example

What is the expected behavior?

Requests to the /ws endpoint should be upgraded to a websocket.

What do you see instead?

hono-ws-error

Additional information

No response

@ronkeiser ronkeiser added the bug label Jun 1, 2024
@ronkeiser
Copy link
Contributor Author

It seems all that needs to happen is that status: 101 needs to be added to the Response:

// src/adapter/cloudflare-workers/websocket.ts

...

return new Response(null, {
      status: 101,
      webSocket: client,
});

@yusukebe, I can create a PR to this effect if this is acceptable.

@ronkeiser
Copy link
Contributor Author

I went ahead and made a PR here:

#2886

@pdlug
Copy link

pdlug commented Jun 3, 2024

@yusukebe any chance you could push a quick release with this fix? I have a project blocked on this issue.

@yusukebe
Copy link
Member

yusukebe commented Jun 3, 2024

@pdlug Yes sir!

@yusukebe
Copy link
Member

yusukebe commented Jun 3, 2024

Hi @ronkeiser @pdlug

I've released the new version v4.4.3 which includes the fix now. Try it!

@pdlug
Copy link

pdlug commented Jun 3, 2024

@yusukebe Working great now. Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants