-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
implement "Upgrade" as a function in a api route to support websocket #12358
Comments
Would a conditional in the fallback method handler fit your needs? https://kit.svelte.dev/docs/routing#server-fallback-method-handler . Our motivation for adding this was so that sveltekit wouldn't need to check an indefinite list of named exports in the +server file (we only focus on the most commonly used ones) |
It wouldn't really help, no. Web Sockets are a two-way stream, and you can't make them fit into a This is basically #1491. It's a hard problem to come up with an API for, and we don't have a solution for it. |
That is not how websocket works (read the link in the OP), the UPGRADE request is not a HTTP method, I suggest this handler name because to me it fits more how the kit uses api routes. |
actually websockets are designed to piggy back on request/response framework, how we expose things to the developer is either we create a specific context per websocket connection and a singler handler function or a function factory that creates a PERMANENT lived function context for the duration of the websocket connection (the latter my proposed solution) Fallback would not work because a websocket initial request is just a I looked at kit code and you can add specific handler names if you want (ofc you need to change skit code for this). |
So basically if kit receives GET + websocket upgrade headerss -> call |
Thanks for all the information on websockets, especially since I don't have much understanding of it yet. Is it a good idea for the framework to handle these upgrades and for the developer to simply export a websocket function to handle these connections? But, reading the MDN docs on this it seems like more control is needed depending on the raw request or just the Might be a good idea to move this discussion to #1491 so that we can collect these ideas and brainstorm an API for this. |
Last post here, moved to #1491
No the upgrade header is what makes this piggy back on existing http transports (remember the billions of routers and proxies that dont need to be configured to handle websockets). other then this you enter the websocket space and ofc you have full duplex control mechancism and framing protocol you can read about full websocket here rfc6455 From Server perspective you need to:
From Client perspective:
there is no crazy magic going on. |
Describe the problem
One can easy implement GET, PUT in an api route by a function (see below)
Describe the proposed solution
If we implement UPGRADE Protocol_upgrade_mechanism we could implement websockets aswell
this file is in
src/route/chat-app/server.js
connect withnew WebSocket('/chap-app')
The text was updated successfully, but these errors were encountered: