🤗 [Question]: Fiber v3 sse
Question Description
Hello all, can we have please a working example for proper event stream in fiber v3? All i have found are v2 partial examples. I've found this using 'pipe' but i don't know if this is the right way of doing it as 'pipe' it's blocking.
Code Snippet (optional)
func Events(c fiber.Ctx) error {
pr, pw := io.Pipe()
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
go func() {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
select {
case t := <-ticker.C:
_, err := fmt.Fprintf(pw, "data: Time is %s\n\n", t.Format(time.RFC3339))
if err != nil {
pw.Close()
return
}
}
}
}()
return c.SendStream(pr)
}
Checklist:
- [x] I agree to follow Fiber's Code of Conduct.
- [x] I have checked for existing issues that describe my questions prior to opening this one.
- [x] I understand that improperly formatted questions may be closed without explanation.
@mhclaudiu Have you tried https://github.com/gofiber/recipes/tree/master/sse ?
Thank you, however that one is for v2.
Thank you, however that one is for v2.
We have an issue opened for updating the example to use c.SendStreamWriter(): https://github.com/gofiber/recipes/issues/2709 for when v3 releases.
We haven't made a PR for it yet, but I think it should be quick to do once we start the migration.