-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Native support for Websockets #12961
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1c6067e
Saving progress on websockets for discussion
LukeHagar ffe1ead
functional implementation
LukeHagar b0f2ee1
cleaned logs
LukeHagar 6972d8f
swapped test app to basics
LukeHagar ff4097e
Fixed formatting overreach
LukeHagar 6d5fb17
swapped to options-2 due to strange handle collision, implemented sug…
LukeHagar cf06d39
simplified example
LukeHagar f28ff1c
restore previous formatting
LukeHagar cb2458e
fixed basics deps
LukeHagar f9806b7
removed un-needed parenthesis
LukeHagar 1bdcc5f
updated lockfile
LukeHagar 8832329
cleaning and formatting
LukeHagar b3ce426
fixed exports test
LukeHagar 44e84ae
regenerated types
LukeHagar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
packages/kit/test/apps/options-2/src/routes/ws/+page.svelte
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script> | ||
import { browser } from '$app/environment'; | ||
import { base } from '$app/paths'; | ||
|
||
let messages = []; | ||
|
||
if (browser) { | ||
const socket = new WebSocket(`${base}/ws`); | ||
|
||
console.log(socket); | ||
|
||
socket.onopen = () => { | ||
console.log('websocket connected'); | ||
socket.send('hello world'); | ||
}; | ||
|
||
socket.onclose = () => { | ||
console.log('disconnected'); | ||
}; | ||
|
||
socket.onmessage = (event) => { | ||
console.log(event.data); | ||
messages = [...messages, event.data]; | ||
console.log(messages); | ||
}; | ||
} | ||
</script> | ||
|
||
<h1>Messages:</h1> | ||
|
||
<div> | ||
{#each messages as message} | ||
<p>{message}</p> | ||
{/each} | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { WebSocketServer } from 'ws'; | ||
|
||
const wss = new WebSocketServer({ noServer: true }); | ||
|
||
wss.on('connection', (ws) => { | ||
ws.on('error', console.error); | ||
|
||
ws.on('message', (message) => { | ||
console.log('received: %s', message); | ||
ws.send(String(message)); | ||
}); | ||
}); | ||
|
||
export function UPGRADE({ upgrade }) { | ||
wss.handleUpgrade(upgrade.request, upgrade.socket, upgrade.head, (ws) => { | ||
console.log('UPGRADED'); | ||
wss.emit('connection', ws, upgrade.request); | ||
}); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not that familiar with
UPGRADE
, but is it true that it sends no response? A quick peek at MDN makes it appear to me that it does send a responsehttps://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade#overview
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/websockets/ws/blob/68614728c164eb55462ff12ba24cb30451d28ec6/lib/websocket-server.js#L379-L414
The response appears to be sent back through the socket itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There does appear to be some variance in the way the response is sent though, for example Bun does not send a response, but Deno does appear to return a standard HTTP response.
Socket.IO also returns a response but they refuse to just give you the resp object, and instead insist on binding directly to a Server so they can send it themselves.
If we look at the Node.JS server upgrade event, they specify the data is sent back through the socket, and their on upgrade handler does not even support sending a standard response object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love maintainer/community input here, its definitely a fun issue to try and solve for everyone :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like Socket.IO may actually support the same interface
https://github.com/socketio/socket.io/blob/91e1c8b3584054db6072046404a24e79a17c1367/packages/engine.io/README.md?plain=1#L40-L58
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't had any luck getting this to work locally
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I hadn't event noticed on first glance that UPGRADE is not an http verb like the others and it's an http header. I'm afraid I'm not terribly familiar with web sockets. I think we'll need to find another way of expressing this that doesn't mirror the API we use for HTTP verbs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood!
One thing that's worth mentioning is that this approach allows for different websocket handlers to be created on different routes, and for all other aspects of the requests and routing to use the existing SK response handling.
The incoming request is different, as the servers don't appear to pass these get requests through to middleware, so the existing middleware handling had to be somewhat copied to the
server.on("upgrade")
handler.