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

Add writeSSE / stream at the Context level for increased DX with SSEs #3527

Open
mirshko opened this issue Oct 18, 2024 · 0 comments
Open

Add writeSSE / stream at the Context level for increased DX with SSEs #3527

mirshko opened this issue Oct 18, 2024 · 0 comments
Labels
enhancement New feature or request.

Comments

@mirshko
Copy link

mirshko commented Oct 18, 2024

What is the feature you are proposing?

I opened this discussion a while back (https://github.com/orgs/honojs/discussions/3472) to see if there were ways around the current streamSSE while loop for callback based SSE sending.

For a project I'm working on the SDK requires a onBlock callback that is called when new data comes in, to which I want to be able to send that data to a client with SSEs.

See:

app.get("/sse", async (c) => {
  const target = c.sendEvents();

  const unwatch = publicClient.watchBlocks( 
    { onBlock: block => target.dispatchMessage(block); }
  )
  
  target.addEventListener("close", (evt) => {
    unwatch()
  });
  
  // ...
})

This model is heavily based on the Oak approach seen here: https://oakserver.github.io/oak/sse.html.

If we could have a similar api approach for a Hono ServerSentEventTarget baked into Context, that would open this connection and keep it open until otherwise closed (in a client disconnect event) that would be ideal.

I couldn't find a way to implement my above code with the current streamSSE since it requires a while loop to remain open.

@maou-shonen mentioned https://github.com/honojs/middleware/tree/main/packages/event-emitter however, it only works for inside the app, but its API is very ideal I would imagine for this feature.

@mirshko mirshko added the enhancement New feature or request. label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

1 participant