-
Thank you for creating this amazing library!
I used this code although it works, echo gives me a warning, but when I use the r3labs/sse library in combination with echo I don't have this problem. So I can't determine if this is an echo problem or a go-sse library problem, can you help me locate it.
working with r3labs/sse code |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi there, thank you for opening this discussion! I've investigated this and found the cause: internally, You shouldn't have see this warning every time, though – it is annoying and may distract attention from important warnings. Here's a way you can integrate package main
import (
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/tmaxmax/go-sse"
)
func main() {
e := echo.New()
s := &sse.Joe{}
go func() {
for range time.Tick(time.Second) {
// Update
ev := &sse.Message{}
time.Sleep(3 * time.Second)
ev.AppendData("time: " + time.Now().Format(time.RFC3339Nano) + uuid.New().String())
_ = s.Publish(ev, []string{sse.DefaultTopic})
}
}()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/sse", func(c echo.Context) error {
log.Printf("SSE client connected, ip: %v", c.RealIP())
go func() {
<-c.Request().Context().Done() // Received Browser Disconnection
log.Printf("The client is disconnected: %v\n", c.RealIP())
}()
sess, err := sse.Upgrade(c.Response(), c.Request())
if err != nil {
c.Error(fmt.Errorf("initialize SSE connection: %w", err))
}
// Let Echo write the header – this is what removes the warning.
// If you remove this line the warning will reappear.
c.Response().WriteHeader(http.StatusOK)
return s.Subscribe(c.Request().Context(), sse.Subscription{
Client: sess,
LastEventID: sess.LastEventID,
Topics: []string{sse.DefaultTopic},
})
})
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
} Note the replacement of If unfamiliar with what a I'll most probably modify the library code in such a way that this warning won't occur regardless of usage, as for now I wouldn't want to document or create integrations with specific libraries, as it is very easy to do them. It might be valuable though to mention Anyway, let me know if this helps and if I can assist you further with anything else! Apologies for the delayed response. |
Beta Was this translation helpful? Give feedback.
Hi there, thank you for opening this discussion!
I've investigated this and found the cause: internally,
go-sse
flushes the request without explicitly writing the header, which triggers an implicit 200 OK write. This implicit write though doesn't toggle some internal Echo flags which then causes Echo to write the header again, hence the "superfluous" write. This is benign and you don't need to worry.You shouldn't have see this warning every time, though – it is annoying and may distract attention from important warnings. Here's a way you can integrate
go-sse
withecho
without such warnings (used your code as starting point):