Repository navigation
How to use WebSockets with the Vercel adapter? #16652
Replies: 4 comments 3 replies
|
WebSocket support is in limbo, see #12973
|
|
Worth separating what's actually in limbo from what already exists. The Vercel platform side is real though, and it isn't Next.js-specific. import { experimental_upgradeWebSocket, type WebSocketData } from '@vercel/functions'
export async function GET() {
return experimental_upgradeWebSocket((ws) => {
ws.on('message', (data: WebSocketData) => {
ws.send(data)
})
})
}A SvelteKit For getting unblocked now rather than experimenting: deploy the socket.io server as its own separate Vercel Function (outside the SvelteKit build, or even a separate minimal Vercel project) using |
|
Short version: you cannot run a WebSocket server on Practical options:
So the app itself can stay on Vercel. The socket server just has to live somewhere persistent. |
|
@MILLERMARRU's snippet is the right call, and the unknown you named is answerable from the package itself. The Response never mattered.
So by the time your handler returns, the 101 is already on the wire and the socket is detached. What comes back is a What I can't confirm from here is the half that actually decides it, which is whether Vercel's launcher populates That makes it cheap to settle. Put one Two things that will bite before you get that far. Last thing, and it's the one that matters most for dropping Pusher. The same doc says new connections aren't guaranteed to reach the same function instance, and tells you to keep rooms, presence and pub/sub in an external store. A module-level |
Uh oh!
There was an error while loading. Please reload this page.
I would like to experiment with WebSockets. Our application is delpoyed using the official Vercel adapter.
Now that Vercel announced support for websockets, this made me think - is there an official way to use websockets in Sveltekit when using the Vercel adapter?
The end-goal would be to build something simple with socket.io, so we don't need a pusher subscription.
All reactions