sse

package module
v0.0.0-...-e2cf2c6 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2020 License: MIT Imports: 11 Imported by: 3

README

GoDoc

sse

-- import "astuart.co/go-sse"

Usage

var Client = &http.Client{}

Client is the default client used for requests.

var (
	//ErrNilChan will be returned by Notify if it is passed a nil channel
	ErrNilChan = fmt.Errorf("nil channel given")
)
var GetReq = func(verb, uri string, body io.Reader) (*http.Request, error) {
	return http.NewRequest(verb, uri, body)
}

GetReq is a function to return a single request. It will be used by notify to get a request and can be replaces if additional configuration is desired on the request. The "Accept" header will necessarily be overwritten.

func Notify
func Notify(uri string, evCh chan<- *Event) error

Notify takes the uri of an SSE stream and channel, and will send an Event down the channel when recieved, until the stream is closed. It will then close the stream. This is blocking, and so you will likely want to call this in a new goroutine (via go Notify(..))

type Event
type Event struct {
	URI  string
	Type string
	Data io.Reader
}

Event is a go representation of an http server-sent event

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrNilChan will be returned by Notify if it is passed a nil channel
	ErrNilChan = fmt.Errorf("nil channel given")

	//Client is the default client used for requests.
	Client = &http.Client{}

	//Logger is used to log debug messages. By default logging is disabled;
	//to enable, use SetOutput() or overwrite this instance.
	Logger = log.New(ioutil.Discard, "", log.LstdFlags)
)
View Source
var GetReq = func(ctx context.Context, verb, uri string) (*http.Request, error) {
	return http.NewRequestWithContext(ctx, verb, uri, nil)
}

GetReq is a function to return a single request. It will be used by notify to get a request and can be replaces if additional configuration is desired on the request. The "Accept" header will necessarily be overwritten.

Functions

func Notify

func Notify(ctx context.Context, uri string, retry bool, evCh chan<- *Event) (err error)

Notify takes the uri of an SSE stream and channel, and will send an Event down the channel when received, until the stream is closed. It will then close the stream. This is blocking, and so you will likely want to call this in a new goroutine (via `go Notify(..)`)

Types

type Event

type Event struct {
	URI  string
	ID   string
	Type string
	Data []byte
}

Event is a go representation of an http server-sent event

Jump to

Keyboard shortcuts

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