sse: github.com/btubbs/sse Index | Files | Directories

package sse

import "github.com/btubbs/sse"

Index

Package Files

client.go event.go event_writer.go parse.go

func Parse Uses

func Parse(r io.Reader, f func(Event))

Parse takes a reader and a callback function. It reads the stream and parses out SSE event payloads, builds an Event struct for each, and passes it into the provided callback.

func Subscribe Uses

func Subscribe(req *http.Request, f func(Event), options ...ClientOption) error

Subscribe is a shortcut for instantiating your own Client and calling its .Subscribe method.

type Client Uses

type Client struct {
    http.Client
    RetryDuration time.Duration
    LastID        string
}

Client works like a regular http.Client, with an additional Subscribe method for connecting to Server Sent Event streams.

func (*Client) Subscribe Uses

func (c *Client) Subscribe(req *http.Request, f func(Event), options ...ClientOption) error

Subscribe performs the provided request, then passes the response into the SSE parser, calling the provided callback with each Event parsed from the stream.

type ClientOption Uses

type ClientOption func(*clientOptions)

A ClientOption is a function that can be passed as an argument to Client.Subscribe to alter its behavior.

func AutoRetry Uses

func AutoRetry(retry bool) ClientOption

AutoRetry takes a bool that flags whether the client should automatically reconnect on errors. The ClientOption that you get back should be passed into Client.Subscribe.

func LastEventID Uses

func LastEventID(id string) ClientOption

LastEventID takes the ID that should be passed to the server to start the stream where you left off. The ClientOption that you get back should be passed into Client.Subscribe.

type Event Uses

type Event struct {
    ID    string
    Event string
    Retry time.Duration
    Data  []byte
}

func (Event) Bytes Uses

func (e Event) Bytes() []byte

Bytes serializes the Event to a byte slice suitable for writing out to the wire as a valid SSE event.

type EventWriter Uses

type EventWriter struct {
    // contains filtered or unexported fields
}

EventWriter is a helper for writing events to an http.ResponseWriter stream.

func NewEventWriter Uses

func NewEventWriter(w http.ResponseWriter) *EventWriter

NewEventWriter returns a new EventWriter.

func (*EventWriter) Write Uses

func (evw *EventWriter) Write(e Event) error

Write takes an Event, serializes the Event to bytes, and writes them out to the http stream. If the stream is also an http.Flusher, then it will also flush the bytes out so the event will be sent over the wire immediately. It automatically sets the Content-Type header to text/event-stream.

Directories

PathSynopsis
client_example
server_example

Package sse imports 7 packages (graph) and is imported by 2 packages. Updated 2018-10-20. Refresh now. Tools for package owners.