sse

package
v0.0.0-...-3597e88 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

This package contains the Server Sent Events implementation used by horizon.

Index

Constants

View Source
const HeartbeatDelay = 5 * time.Second

HeartbeatDelay represents the amount of time a stream will wait before sending a new heartbeat down an idle stream.

Variables

This section is empty.

Functions

func Pumped

func Pumped() <-chan struct{}

Pumped returns a channel that will be closed the next time the input pump sends. It can be used similar to `ctx.Done()`, like so: `<-sse.Pumped()`

func Tick

func Tick()

Tick triggers any open SSE streams to tick by replacing and closing the `nextTick` trigger channel.

func WriteEvent

func WriteEvent(ctx context.Context, w http.ResponseWriter, e Event)

WriteEvent does the actual work of formatting an SSE compliant message sending it over the provided ResponseWriter and flushing.

func WriteHeartbeat

func WriteHeartbeat(ctx context.Context, w http.ResponseWriter)

WriteHeartbeat emits a "heartbeat" comment into the sse stream. Low traffic connections will be kept alive longer if a regular heartbeat is sent regularly.

func WritePreamble

func WritePreamble(ctx context.Context, w http.ResponseWriter) bool

WritePreamble prepares this http connection for streaming using Server Sent Events. It sends the initial http response with the appropriate headers to do so.

Types

type Event

type Event struct {
	Data    interface{}
	Error   error
	ID      string
	Event   string
	Comment string
	Retry   int
}

Event is the packet of data that gets sent over the wire to a connected client.

func (Event) SseEvent

func (e Event) SseEvent() Event

SseEvent returns the SSE compatible form of the Event... itself.

type Eventable

type Eventable interface {
	// SseEvent returns the SSE compatible form of the implementer
	SseEvent() Event
}

Eventable represents an object that can be converted to an SSE compatible event.

type Stream

type Stream interface {
	TrySendHeartbeat()
	Send(Event)
	SentCount() int
	Done()
	SetLimit(limit int)
	IsDone() bool
	Err(error)
}

Stream represents an output stream that data can be written to

func NewStream

func NewStream(ctx context.Context, w http.ResponseWriter, r *http.Request) Stream

NewStream creates a new stream against the provided response writer

Jump to

Keyboard shortcuts

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