Skip to content
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

feat: native support for Websockets #12973

Open
wants to merge 72 commits into
base: main
Choose a base branch
from
Open

Conversation

LukeHagar
Copy link
Contributor

@LukeHagar LukeHagar commented Nov 8, 2024

This PR is a replacement to #12961 with a completely different Websocket implementation using crossws that should be fully compatible with all major runtimes.

Functionality has been validated locally using basic tests in the options-2 test app.

Here is the new usage experience.
+server.js

import { error, accept } from '@sveltejs/kit';

export const socket = {
	upgrade(req) {
		 // Accept the websocket connection with a return
		return accept();

                // Reject the websocket connection with an error
                error(401, 'unauthorized');
	},

	open(peer) {
		//... handle socket open
	},

	message(peer, message) {
		//... handle socket message
	},

	close(peer, event) {
		//... handle socket close
	},

	error(peer, error) {
		//... handle socket error
	}
};

The newest implementation allows different sets of handlers to be implemented on a per-route basis. I have tested some basic uses of websockets locally to much success.

This PR is intended to:
Resolve #12358
Resolve #1491

Steps left

  • Ensure handle runs before upgrading requests
  • Gather feedback
  • Add or update tests
  • Fix the types
  • Update the adapters
  • Update the documentation
  • Add a changeset

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

…ionality for different handlers at different URLs, added example use cases to options-2 test app, added upgrade function for supporting additional adapters, and much more.
Copy link

changeset-bot bot commented Nov 8, 2024

⚠️ No Changeset found

Latest commit: 1ac7ccf

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@LukeHagar LukeHagar mentioned this pull request Nov 8, 2024
6 tasks
@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-kit-12973-svelte.vercel.app/

this is an automated message

@eltigerchino eltigerchino changed the title Native support for Websockets feat: native support for Websockets Nov 11, 2024
@eltigerchino eltigerchino added the feature / enhancement New feature or request label Nov 11, 2024
@eltigerchino eltigerchino marked this pull request as draft November 11, 2024 03:18
@pi0
Copy link

pi0 commented Jan 22, 2025

@LukeHagar LMK, if you need any help with this (you can reach me also on discord @pi0)

@LukeHagar LukeHagar marked this pull request as ready for review January 23, 2025 14:52
@@ -20,7 +20,7 @@
"dropcss": "^1.0.16",
"svelte": "^5.2.9",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Copy link
Member

@eltigerchino eltigerchino Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test apps were using TS 5.5.4 because it was needed when we upgraded them to use Svelte 5. Running pnpm check doesn't reveal the type errors now because I think the pnpm-lock file is using TS 5.7.3 for everything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I saw 5.5.3 some places and 5.5.4 some places. Perhaps we should standardize on 5.5.4 then

@@ -20,15 +21,15 @@ let init_promise;

export class Server {
/** @type {import('types').SSROptions} */
#options;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for removing the private access modifier?

@eltigerchino
Copy link
Member

eltigerchino commented Feb 4, 2025

There's some logic that's missing and might be important before the handle hook runs (mostly to do with populating the event object and whatever before_handle does). They're in the respond function but not the new resolve function.

/** @type {import('types').TrailingSlash | void} */
let trailing_slash = undefined;

// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
// regardless of the `trailingSlash` route option
if (url.pathname === base || url.pathname === base + '/') {
trailing_slash = 'always';

trailing_slash = node.trailingSlash;

if (state.before_handle || state.emulator?.platform) {
let config = {};
/** @type {import('types').PrerenderOption} */
let prerender = false;
if (route.endpoint) {
const node = await route.endpoint();
config = node.config ?? config;
prerender = node.prerender ?? prerender;
} else if (route.page) {
const nodes = await load_page_nodes(route.page, manifest);
config = get_page_config(nodes) ?? config;
prerender = get_option(nodes, 'prerender') ?? false;
}
if (state.before_handle) {
state.before_handle(event, config, prerender);
}
if (state.emulator?.platform) {
event.platform = await state.emulator.platform({ config, prerender });
}
}

const { cookies, new_cookies, get_cookie_header, set_internal } = get_cookies(
request,
url,
trailing_slash ?? 'never'
);
cookies_to_add = new_cookies;
event.cookies = cookies;
event.fetch = create_fetch({
event,
options,
manifest,
state,
get_cookie_header,
set_internal
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement "Upgrade" as a function in a api route to support websocket Native support for web sockets
5 participants