Documentation ¶
Overview ¶
Package sse provides support for Server-Sent Events, as a handler for net/http, and a client that can read SSE events from net/http responses.
SSE is specified here: http://www.w3.org/TR/eventsource/
The package implements only partial support for SSE, specifically the client and server do not handle retries or event buffering.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotAcceptable = errors.New("sse: invalid content-type")
ErrNotAcceptable occurs when the incoming request does not Accept text/event-stream
Functions ¶
func Start ¶
func Start(w http.ResponseWriter, r *http.Request) error
Start asserts that the client wants SSE, and if so, begins writing an SSE stream with the appropriate response headers. It performs a forced flush on the stream to ensure that the client receives the headers immediately.
Example ¶
http.HandleFunc("/example/startsse", func(w http.ResponseWriter, r *http.Request) { err := Start(w, r) if err != nil { w.WriteHeader(http.StatusNotFound) return } ticker := time.NewTicker(time.Second) for { select { case <-ticker.C: helloWorld := &Event{Data: []byte("hello world")} helloWorld.WriteTo(w) case <-r.Context().Done(): ticker.Stop() return } } })
Output:
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides a handle to methods that are bound to a single SSE stream.
type Event ¶
Event represents a single Server-Sent Event.
type ProtocolError ¶
ProtocolError provides details of any protocol error that has occurred, the original Response object and an Error that contains a description of the problem.
func (*ProtocolError) Error ¶
func (pe *ProtocolError) Error() string