0.1.4 • Published 9 months ago

sveltekit-server-sent-events v0.1.4

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

SvelteKit SSE

This library provides an easy way to produce and consume server sent events.

Install with:

npm i -D sveltekit-server-sent-events

Create your server sent event with:

// src/routes/event/+sever.js
import { event } from 'sveltekit-server-sent-events'

/**
 * @param {number} milliseconds
 * @returns
 */
const delay = milliseconds => new Promise(r => setTimeout(r, milliseconds))

export function GET() {
	return event(async emit => {
		while (true) {
			emit(Date.now())
			await delay(1000)
		}
	}).toResponse()
}

and consume it on your client with:

<script>
	// src/routes/+page.svelte
	import { source } from 'sveltekit-server-sent-events'
	const value = source('/event').onError(error => console.error({ error }))
</script>
{$value}
0.1.4

9 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago