Documentation
¶
Overview ¶
Package sse serves Server-Sent Events: a one-way text/event-stream from server to client. Handler wraps a streaming writer; the supplied function owns the send loop and returns to end the stream.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
Handler returns a Fiber handler that streams events. fn is called with a Stream; when fn returns, the stream closes. fn owns its own loop and should stop when a Send returns an error (client gone) or the request context is done.
Example ¶
package main
import (
"time"
"github.com/gofiber/fiber/v2"
"github.com/rahmadafandi/fibr/sse"
)
func main() {
app := fiber.New()
app.Get("/events", sse.Handler(func(c *fiber.Ctx, s *sse.Stream) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for n := 0; ; n++ {
if err := s.Send("tick", map[string]int{"n": n}); err != nil {
return // client disconnected
}
select {
case <-c.Context().Done():
return
case <-ticker.C:
}
}
}))
_ = app
}
Output:
Types ¶
type Event ¶
Event is a fully specified SSE event. Data is JSON-encoded unless it is a string or []byte, which are written verbatim.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream writes SSE frames to a single client connection.
func (*Stream) Comment ¶
Comment writes an SSE comment line (": text"), commonly used as a keepalive.
func (*Stream) Event ¶
Event writes a fully specified event and flushes. A flush error indicates the client disconnected.