/sse

Server-Sent Events (SSE) library for Go

Primary LanguageGoMIT LicenseMIT

sse

build-img pkg-img reportcard-img coverage-img

Server-Sent Events (SSE) library for Go.

See https://www.w3.org/TR/eventsource for the technical specification.

Features

  • Simple API.
  • Performant.
  • Dependency-free.
  • Low-level API to build a server.

Install

Go version 1.13

go get github.com/cristalhq/sse

Example

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.

Documentation

See these docs.

License

MIT License.