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

📝 docs: Added documentations about Server Sent Events (SSE) #5701

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions documentation/docs/03-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,73 @@ assert.equal(
```

To express a `%` character, use `%25`, otherwise the result will be malformed.

### Server Sent Events (SSE)

It is possible to connect your clients to Sveltekit and set it up to send events from the server to your clients. This is also known as [Server-Sent Events (SSE)](https://en.wikipedia.org/wiki/Server-sent_events). This is an alternative to WebSockets, and is supported by all modern browsers. SSE can be implemented as follows, with a chat application as example:

```js
/// file: src/routes/[id].js
// Store all connections in a set-map
const clients = new Set();

export const GET = ({ params }) => {
let controller;

return {
// These headers are important for the browser to detect a SSE request
headers: {
'Content-Type': 'text/event-stream',
Connection: 'keep-alive',
'Cache-Control': 'no-cache'
},
body: new ReadableStream({
start: (_) => {
controller = _;
clients.add({ id: params['id'], connection: controller });
},
cancel: () => {
clients.delete({ id: params['id'], connection: controller });
}
})
};
};

export const POST = async ({ request, params }) => {
const encoder = new TextEncoder();
const message = await request.text();
for (const { id, connection } of clients) {
if (id === params['id']) {
continue;
}

// First format the message correctly with 'data: ' as prefix and 2 new lines as suffix
// Then encode the message to a Uint8Array to be sent to the client
connection.enqueue(encoder.encode('data: ' + message + '\n\n'));
}

return { status: 204 };
};
```

```html
/// file: src/routes/index.svelte

<script lang="ts">
import { onMount } from 'svelte';

onMount(() => {
const events = new EventSource('/<ID of client>');
events.onmessage = (event) => {
console.log(JSON.parse(event.data));
};

setInterval(async () => {
await fetch('/<ID of client>', {
method: 'POST',
body: 'Hello world!'
});
}, 3000);
});
</script>
```