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

RPC support for SSE #3309

Open
cybercoder-naj opened this issue Aug 22, 2024 · 15 comments · May be fixed by #3957
Open

RPC support for SSE #3309

cybercoder-naj opened this issue Aug 22, 2024 · 15 comments · May be fixed by #3957
Labels
enhancement New feature or request.

Comments

@cybercoder-naj
Copy link

What is the feature you are proposing?

We require using EventSource to work with SSE routes, but can we have Hono's RPC to have this functionality as well?

const client = hc<typeof app>(`https://${BASE_URL}`)
client.sse.$get({
  onMessage(event) { /* do something with this */ },
  // other callbacks
}, {
  headers: {
    Authorization: 'Bearer booyah'
  }
})
@cybercoder-naj cybercoder-naj added the enhancement New feature or request. label Aug 22, 2024
@yusukebe
Copy link
Member

yusukebe commented Sep 1, 2024

This is super interesting.

@sor4chi @nakasyou @watany-dev @hagishi and others:

Any thoughts? Or can you work on it?

@nakasyou
Copy link
Contributor

nakasyou commented Sep 1, 2024

I think it is a good idea!

Also I thought, what do you think about typed JSON SSE?
Now, SSE is used to stream LLM content. If we implement JSON SSE, development experience to create LLM app may get better.

@yusukebe
Copy link
Member

yusukebe commented Sep 1, 2024

JSON SSE

Great! As you mentioned, it is used in LLM apps often.

@rayli09
Copy link

rayli09 commented Sep 26, 2024

following

@sor4chi
Copy link
Contributor

sor4chi commented Oct 3, 2024

Hi, @yusukebe
I’m personally working on this development, but EventSource-related APIs are not part of the Web Standards. As a result, writing code that depends on it leads to errors when loading the Hono Client in various runtime environments such as Node.js. Could you suggest a good solution for this?

image

@yusukebe
Copy link
Member

Hi @sor4chi

As a result, writing code that depends on it leads to errors when loading the Hono Client in various runtime environments such as Node.js. Could you suggest a good solution for this?

This means there are differences between the runtime APIs for SSE, right?

@naydenoff
Copy link

This would be really useful for LLM structured responses. Is it still in the plans to be added?

@sor4chi
Copy link
Contributor

sor4chi commented Feb 1, 2025

@yusukebe
Sorry, I didn't realize your response.
Yes, I think so.

@elct9620
Copy link

elct9620 commented Feb 9, 2025

I am following this package to implement SSE support for my PoC today.
https://github.com/Azure/fetch-event-source

How about this version to merge into Hono?
https://gist.github.com/elct9620/e7f34dbe22670eec5493b6503769c5a7

P.S. didn't have the error handler and abort controller yet.

I think we have support for the POST method so that users can send messages to LLM and get response streaming.

@yusukebe
Copy link
Member

@sor4chi What do you think of the @elct9620 's implementation?

@sor4chi
Copy link
Contributor

sor4chi commented Feb 18, 2025

Are you assuming that you are going to add faet-event-source as a deps in Hono? Or do you mean copying that implementation into Hono and using it?

@elct9620
Copy link

In my opinion, we need to copy or re-implement a similar behavior into Hono.

The fetch-event-source will call fetch directly and wrapper server event. However, Hono RPC has its API call method, this is why I re-implement it to make it work with Hono RPC.

I assume the API may be

const res = await agentApi[':name'].conversation.$post(...)

await res.sse({ // SSE is a type of response can be formatted like JSON
  onMessage: event => { ... }
})

@eliellis
Copy link

eliellis commented Feb 21, 2025

In my opinion, we need to copy or re-implement a similar behavior into Hono.

The fetch-event-source will call fetch directly and wrapper server event. However, Hono RPC has its API call method, this is why I re-implement it to make it work with Hono RPC.

I assume the API may be

const res = await agentApi[':name'].conversation.$post(...)

await res.sse({ // SSE is a type of response can be formatted like JSON
onMessage: event => { ... }
})

Just adding to the discussion here, an RCP API could also be AsyncGenerator-based.

See Elysia's Eden Treaty client API for streaming responses as a unit of prior art / inspiration for something like this.

@sor4chi
Copy link
Contributor

sor4chi commented Feb 21, 2025

Thank you, everyone! I’ll try implementing it next week.

@sor4chi
Copy link
Contributor

sor4chi commented Feb 25, 2025

I tried https://github.com/Azure/fetch-event-source, but it depends on the document, so in the end, I had to polyfill global.

Azure/fetch-event-source#39

For now, I will add the eventsource polyfill and present the POC, and then leave the rest to @yusukebe’s discretion.

@sor4chi sor4chi linked a pull request Feb 25, 2025 that will close this issue
8 tasks
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

Successfully merging a pull request may close this issue.

8 participants