sse

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2022 License: MIT

README

Go client for HTML5 Server-Sent Events

GoDoc

The package provides fast primitives to manipulate Server-Sent Events as defined by the HTML5 spec.

Get the package with go get github.com/go-rfc/sse

Check the contributing guidelines when submitting changes.

Usage

import "github.com/go-rfc/sse/pkg/eventsource"

es, err := eventsource.New("http://foo.com/stocks/AAPL")

for {
    select {
    case event := <-es.MessageEvents():
        log.Printf("[Event] ID: %s\n Name: %s\n Data: %s\n\n", event.ID, event.Name, event.Data)
    case state := <-es.ReadyState():
        log.Printf("[ReadyState] %s (err=%v)", state.ReadyState, err)
    }
}

The library includes WithBasicAuth and WithAuthorizationBearer modifiers.

eventsource.New("http://foo.com/stocks/AAPL", eventsource.WithBasicAuth("user", "password"))
eventsource.New("http://foo.com/stocks/AAPL", eventsource.WithAuthorizationBearer("token"))

Create your own RequestModifier in case you need further manipulation of the underlying HTTP request.

Decoder

The decoder package allows decoding events from any io.Reader source

import "github.com/go-rfc/sse/pkg/decoder"

resp, _ := http.Get("http://foo.com/stocks/AAPL")
decoder = decoder.New(resp.Body) // any io.Reader works
for {
    event, err := decoder.Decode()
}

Encoder

The encoder package allows encoding a stream of events

import "github.com/go-rfc/sse/pkg/encoder"

event := &base.MessageEvent{
    ID:   "some id",
    Name: "stock-update",
    Data: "AAPL 30.09",
}

sut.WriteComment("example event")
sut.WriteRetry(1000)
sut.WriteEvent(event)

Directories

Path Synopsis
cmd
internal
pkg
eventsource
Eventsource package provides the utilities to connect and process events from an HTML5 Server-Sent Events endpoint.
Eventsource package provides the utilities to connect and process events from an HTML5 Server-Sent Events endpoint.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL