diff --git a/README.md b/README.md index 06928cd..62d189e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # server-sent-events Experiments with Server Sent Events + +```sh +deno task start +``` diff --git a/deno.jsonc b/deno.jsonc new file mode 100644 index 0000000..95476cb --- /dev/null +++ b/deno.jsonc @@ -0,0 +1,6 @@ +{ + "lock": false, + "tasks": { + "start": "deno run --watch --allow-net mod.ts" + } +} diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..b1eea24 --- /dev/null +++ b/mod.ts @@ -0,0 +1,83 @@ +import { Hono } from 'https://deno.land/x/hono/mod.ts' +import { html, raw } from 'https://deno.land/x/hono/helper.ts' +import { stream, streamText, streamSSE } from 'https://deno.land/x/hono/helper.ts' + +const app = new Hono() + +app.get('/', (ctx) => { + return ctx.html(html` + + + Test Site + + + + + + + + + `) +}) + +app.get('/request', (ctx) => { + return ctx.json({ + message: "Cool" + }) +}) + +app.get("/sse", (ctx) => { + let id = 0; + return streamSSE(ctx, async (stream) => { + while (true) { + const message = `It is ${new Date().toISOString()}` + await stream.writeSSE({ + data: message, + event: 'time-update', + id: String(id++), + }) + await stream.sleep(1000) + } + }) +}) + + +Deno.serve(app.fetch)