-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
Description
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:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my questions prior to opening this one.
- I understand that improperly formatted questions may be closed without explanation.
Copilot