Using server-sent events with Fresh #2756
canadaduane
started this conversation in
General
Replies: 1 comment
-
Quite interesting! Thx for sharing ❤️ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I had a bit of an adventure trying to use server-sent events in Fresh 2.0 and want to document my journey for others.
Initially, I thought to create a ReadableStream response in a GET or POST handler.
However, when I tried this in Fresh, I received the following server-terminating error:
I believe I was making a mistake on both the client AND the server side. On the client side, because I hadn't quite grokked that an EventSource with
onmessage
listener was required (I was usingfetch
), and on the server because the handler was set up to expect the usual request/response within an HTTP method handler (e.g. GET, POST, DELETE, etc.)The solution is to use EventSource on the client, and just a plain handler on the server, with ReadableStream:
Client, i.e. inside a component:
Server, i.e. /api/sse.tsx:
With this basic structure, you can encode and send data to the client whenever you need to push data. Deno implements HTTP/2, so there is no max stream of 6 connections from server to client, as in HTTP/1.1.
Beta Was this translation helpful? Give feedback.
All reactions