feat(with-realtime-chat): serve the chat socket with upgradeWebSocket - #246
Draft
andrelandgraf wants to merge 1 commit into
Draft
feat(with-realtime-chat): serve the chat socket with upgradeWebSocket#246andrelandgraf wants to merge 1 commit into
andrelandgraf wants to merge 1 commit into
Conversation
The chat function exported `{ fetch, upgrade }` and drove the handshake with the
`ws` library. It now upgrades from inside `fetch` with `upgradeWebSocket` from
`@neon/functions`, which is the interface Neon Functions expose.
const { socket, response } = upgradeWebSocket(request);
clients.add(socket);
socket.addEventListener('message', (event) => { ... });
return response;
`socket` is a standard WebSocket, so the fan-out loop and the client are
unchanged. Auth is the part that reads better: rejecting an unauthenticated
handshake is `return new Response('unauthorized', { status: 401 })` rather than
writing `HTTP/1.1 401 Unauthorized` onto a raw socket and destroying it.
`ws` and `@types/ws` are gone from the dependencies, along with the `node:http`
and `node:stream` type imports. `@neon/functions` moves to ^0.7.0, the first
version that exports `upgradeWebSocket`.
The vendored `neon-functions` skill is refreshed in all nine examples that ship
it, which drops its `hono-websockets.md` reference -- the Hono adapter it
described only existed to bridge Hono onto the raw Node handshake.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
with-realtime-chatexported{ fetch, upgrade }and drove the WebSocket handshake with thewslibrary. It now upgrades from insidefetchwithupgradeWebSocketfrom@neon/functions@0.7.0.socketis a standardWebSocket, so the Postgres poll/fan-out loop and the browser client are untouched.Why it reads better
Auth is the part that changes shape. Refusing an unauthenticated handshake used to mean writing a status line onto a raw socket and destroying it:
Net for
src/index.ts: −27/+23, and these imports are gone entirely:wsand@types/wsare dropped frompackage.json;@neon/functionsmoves^0.5.0→^0.7.0, the first version that exportsupgradeWebSocket.Skill refresh
The vendored
neon-functionsskill is re-installed in all nine examples that ship it (with-ai-sdk,with-hono,with-mastra,with-mcp,with-realtime-chat,with-realtime-sse, and the threebots/*), picking up neondatabase/agent-skills#78. That dropsreferences/hono-websockets.mdfrom each — the vendored Hono adapter existed only to bridge Hono's helper onto the raw Node handshake, and there is no raw handshake to bridge to now.Installed with
npx skills add neondatabase/agent-skills -s neon-functions -a universal, so only the.agents/skillstree each example already tracked is touched.Verification
tsc --noEmitis clean on the rewritten function against the real@neon/functionstypes, under the example's ownstricttsconfig, with no assertions added.The interface itself was exercised against a real
neon devover loopback on an equivalent handler: the 101 with a correct accept key, text fan-out to a second client, binary asArrayBuffer, a 70 KB message across both extended length forms, subprotocol negotiation, server-initiated close with code and reason, ping/pong, and the 401 rejection path.Not verified — read before merging
This example has not been deployed and run end to end on the new interface. The deployed Neon Functions runtime does not yet publish the WebSocket bridge that
upgradeWebSocketreads — that work is LKB-15856, andneon-solutions/function-specrecords the gap as still open. On today's runtime this function will throwTypeError: upgradeWebSocket() is only available inside a Neon Functions invocationat the first handshake.It works under
neon devwithneon@2.45.0. Do not merge until the runtime side ships, or the example's deploy instructions will be broken for anyone following the README.