question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Native support for web sockets

See original GitHub issue

We’ve been attempting to migrate our app from sapper to sveltekit and ran into the problem of web sockets, in sapper we could simply attach the WebSocket server directly to the express server and be done with it, but as per Rich Harris’ comments here and here here in sveltekit we don’t want to expose direct access to the middlwares thus promoting the node adapter over the serverless ones.

However seeing as some of the serverless providers are starting to support web sockets (cloudflare) (begin) (AWS Lambda) should sveltekit provide a way to handle these connections directly?

A native approach to this could also potentially provide a tool to solve this issue and this

Describe the solution you’d like An interesting solution could be to expose functions in endpoints in the same way we handle http requests, and then let the adapters handle the rest

routes/api/index.ts

// Handle post requests
export const post: RequestHandler = async (request) => {....}
// Handle web socket requests
export const ws: WebSocketRequestHandler = async (request) => {....}

A concern with this approach could be mixing protocols, within an endpoint could be confusing. Another could be if this becomes too complicated since as far as I know the initial handshake to start a WebSocket connection is handled over http, would this then be handled directly by sveltekit which could be considered too much “magic” since it would have to intercept a get request that would then not make it to the get handler in the endpoint. Or should we let the user implement the get endpoint and return the WebSocket pair and upgrade header.

Describe alternatives you’ve considered The way we go about it today is a small script that manipulates the build output of sveltekit as such however this does not work in dev mode which is a pain point.

const instance = createServer({ render: app.render }).listen(PORT, HOST, (err) => {
	if (err) {
		console.log('error', err);
	} else {
		console.log(`Listening on port ${PORT}`);
	}
});
app.server.installSubscriptionHandlers(instance);
export { instance };

Is this a feature that you feel would suit sveltekit? are there any considerations I’ve overlooked?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:78
  • Comments:27 (5 by maintainers)

github_iconTop GitHub Comments

32reactions
bluepuma77commented, Feb 17, 2022

@benmccann It would be great if we could get a quick description (or link to it) how we can use WebSocket with SvelteKit.

Specifically for those web developers not so much involved with the inner workings of SvelteKit and Vite.

25reactions
superboss224commented, Nov 29, 2022

As of now, Cloudflare Workers, Deno Deploy and AWS are supporting WebSockets There is gonna be more and more WebSocket support on serverless platforms, having some native and first-class WebSocket support is something we truly need to make immersive experiences that can go realtime

Read more comments on GitHub >

github_iconTop Results From Across the Web

The WebSocket API (WebSockets) - Web APIs - MDN Web Docs
desktop desktop Chrome Edge WebSocket Full support. Chrome4. Toggle history Full support. Edge12. Toggl... WebSocket() constructor Full support. Chrome4. Toggle history Full support. Edge12. Toggl...
Read more >
Web Sockets | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
GitHub - endel/NativeWebSocket: WebSocket client for Unity
WebSocket client for Unity - with no external dependencies (WebGL, Native, Android, ... WebSockets ); WebGL/HTML5 support; Supports all major build targets ...
Read more >
Public preview: Native support for WebSocket APIs in Azure ...
We are announcing the native support for WebSocket APIs (public preview) in Azure API Management. This enables you to manage, protect, ...
Read more >
How to implement WebSockets in React Native
WebSockets is a protocol that provides full-duplex communication, which means the client and server stay connected over a single TCP connection.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found