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

Make WebSocket adapter more changeable #3468

Closed
nakasyou opened this issue Sep 30, 2024 · 0 comments · Fixed by #3531
Closed

Make WebSocket adapter more changeable #3468

nakasyou opened this issue Sep 30, 2024 · 0 comments · Fixed by #3531
Labels
enhancement New feature or request.

Comments

@nakasyou
Copy link
Contributor

What is the feature you are proposing?

I propose createUpgradeWebSocket helper and WSContext class.

For example, you can implement Deno adapter like this:

export const upgradeWebSocket: UpgradeWebSocket = createUpgradeWebSocket((c, events, options) => {
  const { response, socket } = Deno.upgradeWebSocket(c.req.raw, options ?? {})

  const wsContext = new WSContext({
    close: (code, reason) => socket.close(code, reason),
    get protocol() {
      return socket.protocol
    },
    raw: socket,
    get readyState() {
      return socket.readyState as WSReadyState
    },
    url: socket.url,
    send: (source) => socket.send(source)
  })
  socket.onopen = (evt) => events.onOpen?.(evt, wsContext)
  socket.onmessage = (evt) => events.onMessage?.(evt, wsContext)
  socket.onclose = (evt) => events.onClose?.(evt, wsContext)
  socket.onerror = (evt) => events.onError?.(evt, wsContext)

  return response
})

Current issues

Currently, adapter creators have to change code if WSContext or UpgradeWebSocket API interface is changed. Hono provides an interface to implement custom WebSocket adapter. So changing WSContext or UpgradeWebSocket API interface might be a breaking change. For example, #3466 changes an API.

And, WsContext repeats the same implementation between adapters. For example, binaryType: 'arraybuffer' is had by Bun, Deno. The change resolves it.

In order to make WebSocket adapter more changeable, I propose these APIs.

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.

1 participant