/Knowledge/sources/honojs__hono/llms-full/websocket-helper-upgradewebsocket--f2fb40df9e52.md

WebSocket Helper: upgradeWebSocket() - hono-docs

WebSocket Helper: upgradeWebSocket() Source evidence: /Sources/honojs hono/provenance.md Canonical citation: https://hono.dev/llms full.txt upgradewebsocket...

WebSocket Helper: upgradeWebSocket()

Source evidence: /Sources/honojs__hono/provenance.md Canonical citation: https://hono.dev/llms-full.txt#upgradewebsocket

Summary

upgradeWebSocket() returns a handler for handling WebSocket. ```ts const app = new Hono() app.get( '/ws', upgradeWebSocket((c) => { return { onMessage(event, ws) { console.log(Message from client: ${event.data}) ws.send('Hello from s...

Content

upgradeWebSocket() returns a handler for handling WebSocket.

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')
      },
    }
  })
)

Available events:

  • onOpen - Currently, Cloudflare Workers does not support it.
  • onMessage
  • onClose
  • onError

::: warning

If you use middleware that modifies headers (e.g., applying CORS) on a route that uses WebSocket Helper, you may encounter an error saying you can't modify immutable headers. This is because upgradeWebSocket() also changes headers internally.

Therefore, please be cautious if you are using WebSocket Helper and middleware at the same time.

:::

Metadata

{
  "chunk_index": 0,
  "citation": "https://hono.dev/llms-full.txt#upgradewebsocket",
  "coverage_role": "overview",
  "qualified_title": "Hono / llms-full / WebSocket Helper > `upgradeWebSocket()`",
  "retrieved_at": "2026-07-02T05:51:01Z",
  "section_index": 708,
  "source_id": "/honojs/hono",
  "source_type": "llms_full",
  "target_label": "llms-full",
  "title": "WebSocket Helper: upgradeWebSocket()",
  "upstream_url": "https://hono.dev/llms-full.txt",
  "version": "2026"
}
Kinic Wiki
Loading knowledge node
Details

Identity

database
db_23dhmsxlhukv
database_id
db_23dhmsxlhukv
path
/Knowledge/sources/honojs__hono/llms-full/websocket-helper-upgradewebsocket--f2fb40df9e52.md
kind
directory
role
markdown_note
children
0

Metadata

created_at
virtual
updated_at
virtual
etag
virtual
metadata_json
{}

Lint Hints

No lightweight warnings.

Outgoing Links

No outgoing links indexed.

Incoming Links

Select a file node to inspect backlinks.

Raw Source

No raw source path inferred.