-
Notifications
You must be signed in to change notification settings - Fork 513
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
Proposal: add sse extension #688
Comments
I would like to work on this. |
thanks,this is a feature that has real users who are looking forward to using it. @Haswf ❤️ |
For anyone who are interested in this proposal, here is some update. haswf/sse contains sse implementation for Hertz. A few to-dos before pr:
|
I ran into a problem that event.Render (which internally calls resp.AppendBody to write event) doesn't trigger a flush, which is required for server-sent event to work. For example, the following code only send response back to client after 10 seconds has elapled and the 2nd event has been rendered. The expected behaviour is http header is immediately returned with 1st event, then the 2nd event is sent after 10 seconds. Can someone know how Hertz response buffer work helps me out? package main
import (
"context"
"time"
"github.com/Haswf/sse"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
)
func main() {
h := server.Default()
h.GET("/sse", func(ctx context.Context, c *app.RequestContext) {
// client can tell server last event it received with Last-Event-ID header
lastEventID := sse.GetLastEventID(c)
hlog.CtxInfof(ctx, "last event ID: %s", lastEventID)
// you can call Render multiple times to send events to client
c.Render(200, sse.Event{
Event: "message",
Data: map[string]string{
"content": "hello world",
},
})
// this doesn't work. long sigh
c.Flush()
time.Sleep(10 * time.Second)
c.Render(200, sse.Event{
Event: "timestamp",
Data: time.Now().Format(time.RFC3339),
})
})
h.Spin()
} |
Thank you. This problem has been resolved by implementing a StreamBodyWriter according the document you referenced. |
Is your feature request related to a problem? Please describe.
Currently Hertz does not support sse extension
Describe the solution you'd like
Add a sse extension or add a sse example
Additional context
The text was updated successfully, but these errors were encountered: