-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change item polling to use server sent events (#98)
- Loading branch information
Showing
10 changed files
with
200 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
import { EventEmitter } from "node:events"; | ||
|
||
export const itemEmitter = new EventEmitter(); |
This file contains 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,36 @@ | ||
// https://github.com/sanrafa/sveltekit-sse-example/tree/main | ||
import type EventEmitter from "node:events"; | ||
|
||
export function createSSE(retry = 0) { | ||
const { readable, writable } = new TransformStream({ | ||
start(ctr) { | ||
if (retry > 0) ctr.enqueue(`retry: ${retry}\n\n`); | ||
}, | ||
transform({ event, data }, ctr) { | ||
let msg = data?.id ? `id: ${String(data.id)}\n` : ": hi\n\n"; | ||
if (event) msg += `event: ${event}\n`; | ||
if (typeof data === "string") { | ||
msg += "data: " + data.trim().replace(/\n+/gm, "\ndata: ") + "\n\n"; | ||
} else { | ||
msg += `data: ${JSON.stringify(data)}\n\n`; | ||
} | ||
ctr.enqueue(msg); | ||
} | ||
}); | ||
|
||
const writer = writable.getWriter(); | ||
|
||
return { | ||
readable, | ||
async subscribeToEvent(emitter: EventEmitter, event: string) { | ||
function listener(data: any) { | ||
writer.write({ event, data }); | ||
} | ||
emitter.on(event, listener); | ||
await writer.closed.catch((e) => { | ||
if (e) console.error(e); | ||
}); | ||
emitter.off(event, listener); | ||
} | ||
}; | ||
} |
This file contains 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 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 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,17 @@ | ||
import { SSEvents } from "$lib/schema"; | ||
import { itemEmitter } from "$lib/server/events/emitters"; | ||
import { createSSE } from "$lib/server/events/sse"; | ||
import type { RequestHandler } from "./$types"; | ||
|
||
export const GET = (async () => { | ||
const { readable, subscribeToEvent } = createSSE(); | ||
subscribeToEvent(itemEmitter, SSEvents.item.update); | ||
subscribeToEvent(itemEmitter, SSEvents.item.create); | ||
subscribeToEvent(itemEmitter, SSEvents.item.delete); | ||
return new Response(readable, { | ||
headers: { | ||
"cache-control": "no-cache", | ||
"content-type": "text/event-stream" | ||
} | ||
}); | ||
}) satisfies RequestHandler; |
This file contains 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 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 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
Oops, something went wrong.