-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: native support for Websockets #12973
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
base: main
Are you sure you want to change the base?
Changes from 133 commits
d0b7f09
b69b2e0
aa69e1c
38c919c
2a022b5
c3a0bf7
c86e4e9
5858b49
9d56c50
46c8682
7791759
70202e3
92e3e41
db517f6
a43c49b
6469816
ee0c6ee
e737e02
42e2f2d
0abeb17
2a9971f
cc58820
29fdfec
fda8a68
ff58988
1fed29d
0ea72ab
6408350
182a666
dda7298
192840a
ec0b797
f3bed08
2f11dca
3f2679e
de37505
8dfad0c
21cb866
f70aea1
c0a5e3d
a6bcf60
d3a48d7
fa52645
e63dfc3
a0c54ab
c9457a4
51d4729
04b011d
2ab789f
4a6c8b3
9bdfc04
894f38b
e7726a6
8de07d1
c18b4e8
ddc9bc1
80e14fe
3e58143
7563cb3
fd5b98b
e79716c
3f884b1
dea0952
25f039a
4072369
875536b
84a1aa1
93d40b2
24f3b52
8c10aa4
52d607a
1ac7ccf
d9c4b1f
6a3581b
e762693
5c1f334
8f98d61
ac788fb
0a160fd
d6f4ee5
5e07cba
a0629ba
fef9dbe
940eb3d
e6caf6c
d437299
f203b8d
e6fed10
f13af3b
278c283
a458ff3
b766e06
3dc373c
9597c7f
9a839b5
183117f
a9a3f9b
e201b3b
74937fd
68bedce
ec630b8
19f273b
8517855
be4346d
63cfe11
7eb180f
c8de055
6b06673
09c1e2a
2fd6711
b03206d
d997737
06063f1
fef8521
0c1de43
6411a4c
db7f8c8
23ad389
3ffe264
367df8f
3bc744d
31d5617
fd05a21
bf9d9d3
ffadf9a
bb7aeb4
d4ca5eb
2b13bba
427aeef
2b6be27
4f7b7ec
f4e6c05
be2a624
651c15e
c902fe8
304e8a5
63646f3
64fff72
5570227
6e12953
ac17702
6640525
c5c9f0a
5ee9478
74fcc83
2896ecf
7905670
0560640
a72bac6
32981e0
f7a88dd
87c3071
5ec65b0
330c325
9582975
1c314ef
0c497bb
51783ae
737d352
fd956cb
3977af6
fbc9a8a
7d0b219
448f46a
ba58ee7
fe48df9
335f58c
e933ea7
7f74b95
12f7a97
7a6615e
82e35ea
71cb0c5
5d65b6f
e73415c
d122476
71376db
10e9274
5ff6324
5595d34
628323a
16ddd0c
52677d7
d6e64ab
98c2021
5de28ec
0a80c3c
13113c2
be8f67e
5e2bfc8
4d89bba
b34d7c8
4746851
74e2a86
ef0619d
dfaa63d
56b91df
514fb6d
4c0ee68
2b36ccc
13d11c5
c64ce20
4bab022
6ca1034
440e71c
a1f9e05
6931bfa
802d925
f46d2bf
b99608e
1998d7b
cf3361a
ef444e2
23b4f31
20ee709
0402340
8eb64dc
3de9608
2ec6f55
5fbc9c8
8cbc879
03bce3e
b625c85
53ca1fa
1d812e1
d65212c
ed0b01d
276c828
7db389f
3059744
08a3fcc
d8d803f
12d1602
c0a1145
24e8475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,133 @@ | ||||||
--- | ||||||
title: WebSockets | ||||||
--- | ||||||
|
||||||
## The `socket` object | ||||||
|
||||||
[WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) provide a way to open a bidirectional communication channel between a client and a server. | ||||||
|
||||||
A `+server.js` file can export a `socket` object to handle WebSocket connections. It uses [crossws](https://crossws.unjs.io/) to provide a consistent interface across different platforms. You can define [hooks](https://crossws.unjs.io/guide/hooks), all optional, to handle the different stages of the WebSocket lifecycle. | ||||||
|
||||||
```js | ||||||
/** @type {import('@sveltejs/kit').Socket} **/ | ||||||
export const socket = { | ||||||
upgrade(req) { | ||||||
// ... | ||||||
}, | ||||||
|
||||||
open(peer) { | ||||||
// ... | ||||||
}, | ||||||
|
||||||
message(peer, message) { | ||||||
// ... | ||||||
}, | ||||||
|
||||||
close(peer, event) { | ||||||
// ... | ||||||
}, | ||||||
|
||||||
error(peer, error) { | ||||||
// ... | ||||||
} | ||||||
}; | ||||||
``` | ||||||
|
||||||
### upgrade | ||||||
|
||||||
The `upgrade` hook is called before a WebSocket connection is established. It receives the [request](https://developer.mozilla.org/docs/Web/API/Request) object as a parameter. | ||||||
|
||||||
You can use the [`error`](@sveltejs-kit#error) function imported from `@sveltejs/kit` to easily reject connections. Requests will be auto-accepted if the `upgrade` hook is not defined or does not `error`. | ||||||
|
||||||
```js | ||||||
import { error } from "@sveltejs/kit"; | ||||||
|
||||||
/** @type {import('@sveltejs/kit').Socket} **/ | ||||||
export const socket = { | ||||||
upgrade(request) { | ||||||
if (request.headers.get('origin') !== 'allowed_origin') { | ||||||
// Reject the WebSocket connection by throwing an error | ||||||
error(403, 'Forbidden'); | ||||||
} | ||||||
} | ||||||
}; | ||||||
``` | ||||||
|
||||||
### open | ||||||
|
||||||
The `open` hook is called when a WebSocket connection is opened. It receives the [peer](https://crossws.unjs.io/guide/peer) object, to allow interacting with connected clients, as a parameter. | ||||||
|
||||||
```js | ||||||
/** @type {import('@sveltejs/kit').Socket} **/ | ||||||
export const socket = { | ||||||
open(peer) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be a deviation from the crossws API but if we used this signature instead we would be able to access the
Suggested change
Similarly for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love the experience this destructing provides, and love it today with the existing parts of kit |
||||||
// ... | ||||||
} | ||||||
}; | ||||||
``` | ||||||
|
||||||
### message | ||||||
|
||||||
The `message` hook is called when a message is received from the client. It receives the [peer](https://crossws.unjs.io/guide/peer) object, to allow interacting with connected clients, and the [message](https://crossws.unjs.io/guide/message) object, containing data from the client, as parameters. | ||||||
|
||||||
```js | ||||||
/** @type {import('@sveltejs/kit').Socket} **/ | ||||||
export const socket = { | ||||||
message(peer, message) { | ||||||
// ... | ||||||
} | ||||||
}; | ||||||
``` | ||||||
|
||||||
### close | ||||||
|
||||||
The `close` hook is called when a WebSocket connection is closed. It receives the [peer](https://crossws.unjs.io/guide/peer) object, to allow interacting with connected clients, and the close event object, containing the [WebSocket connection close code](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code#value) and reason, as parameters. | ||||||
|
||||||
```js | ||||||
/** @type {import('@sveltejs/kit').Socket} **/ | ||||||
export const socket = { | ||||||
close(peer, event) { | ||||||
// ... | ||||||
} | ||||||
}; | ||||||
``` | ||||||
|
||||||
### error | ||||||
|
||||||
The `error` hook is called when a WebSocket connection error occurs. It receives the [peer](https://crossws.unjs.io/guide/peer) object, to allow interacting with connected clients, and the error as parameters. | ||||||
|
||||||
```js | ||||||
/** @type {import('@sveltejs/kit').Socket} **/ | ||||||
export const socket = { | ||||||
error(peer, error) { | ||||||
// ... | ||||||
} | ||||||
}; | ||||||
``` | ||||||
|
||||||
## Connecting from the client | ||||||
|
||||||
To connect to a WebSocket endpoint, you can use the [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket) constructor in the browser. | ||||||
|
||||||
```svelte | ||||||
<script> | ||||||
import { onMount } from 'svelte'; | ||||||
|
||||||
onMount(() => { | ||||||
// To connect to src/routes/ws/+server.js | ||||||
const socket = new WebSocket(`/ws`); | ||||||
|
||||||
socket.addEventListener("open", (event) => { | ||||||
socket.send("Hello Server!"); | ||||||
}); | ||||||
|
||||||
// ... | ||||||
}); | ||||||
</script> | ||||||
``` | ||||||
|
||||||
See [the WebSocket documentation on MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) for more details. | ||||||
|
||||||
## Compatibility | ||||||
|
||||||
SvelteKit uses [`crossws`](https://crossws.unjs.io) to handle cross-platform WebSocket connections. Please refer to their [compatibility table](https://crossws.unjs.io/guide/peer#compatibility) for the `peer` object in different runtime environments. | ||||||
teemingc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.