Server-Sent Events (SSE) library for Go.
See https://www.w3.org/TR/eventsource for the technical specification.
- Simple API.
- Performant.
- Dependency-free.
- Low-level API to build a server.
Go version 1.13
go get github.com/cristalhq/sse
As a simple HTTP handler:
http.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
stream, err := sse.UpgradeHTTP(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data := struct {
Text string `json:"text"`
}{
Text: "hey there",
}
stream.WriteJSON("123", "msg", data)
})
See example_test.go for more.
See these docs.