-
-
Notifications
You must be signed in to change notification settings - Fork 644
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
feat(helper/streaming): Support Promise<string> or (async) JSX.Element in streamSSE #3344
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## next #3344 +/- ##
==========================================
- Coverage 95.76% 95.76% -0.01%
==========================================
Files 151 151
Lines 9166 9189 +23
Branches 2683 2701 +18
==========================================
+ Hits 8778 8800 +22
- Misses 388 389 +1 ☔ View full report in Codecov by Sentry. |
@yusukebe @watany-dev @sor4chi |
Hey @usualoma It is interesting that JSX can be written in SSE's import { Hono } from 'hono'
import { streamSSE } from 'hono/streaming'
const app = new Hono()
app.get('/', (c) => {
return c.html(
<html>
<head>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]/sse.js"></script>
</head>
<body>
<div hx-ext="sse" sse-connect="/event-source" sse-swap="EventName"></div>
</body>
</html>
)
})
app.get('/event-source', (c) => {
return streamSSE(c, async (stream) => {
while (true) {
await stream.writeSSE({
data: (
<div>
<b>It's</b> {new Date().toISOString()}
</div>
),
event: 'EventName'
})
await stream.sleep(1000)
}
})
})
export default app I like this feature. |
Hello, @yusukebe and @usualoma. I think when I proposed something similar in this PR before, the conclusion was that it was not necessary because it could be done by piping with Is it because you can't use |
@sor4chi Thank youuu. SSE
Yes, the main reason is as you pointed out, SSE cannot simply use streamAs for the stream referenced in #1913, it can be simply piped with renderToReadableStream. And in general, script elements output by Suspense are also executed without any additional implementation. However, there are some APPs that can be written more simply by #1913, and considering the consistency with SSE, I feel that it would be better to reopen #1913 and merge it. (Sorry, I'm submitting this in the middle of a bit of a children's event, but that's the way I see it) |
….Element) to resolveCallback
Thank you for your answer, I see what you mean. In the meantime, I'm all for this idea. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
#3319
This change allows you to use "hono/jsx" to write the following in streamSSE. I don't think there are many use cases, but the changes on the
helper/streaming/sse.ts
side are also small and could be added to the implementation.The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code