sse

package module
v0.0.0-...-8df0257 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: MIT Imports: 4 Imported by: 2

README

sse

Server-sent events event stream for go

Why?

I've written this several different ways several different times in recent history so I figured I'd put a copy up here to save me some effort.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmitOption

type EmitOption func(*event)

EmitOption for being passed to an Emitter's Emit function mostly for altering the behavior or structure of the event

func WithName

func WithName(name string) EmitOption

WithName is an EmitOption that sets the name for a given event to make it possible to make use of the addEventHandler on the EventSource in javascript

Emit("data", WithName("borg"))

source.addEventListener("borg", function(ev) {
    console.log("Oh no, the borg have ", ev.data)
})

type Emitter

type Emitter interface {
	Emit(body interface{}, opts ...EmitOption)
}

Emitters Emit events with Optional arguments

type EventStream

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

EventStream is your handy dandy all in one http.Handler and Emitter

func New

func New(opts ...NewOption) *EventStream

New EventStream will construct a stream and start a goroutine to forward braodcast events in the background

func (*EventStream) Emit

func (e *EventStream) Emit(body interface{}, opts ...EmitOption)

Emit an event

func (*EventStream) Len

func (e *EventStream) Len() int

Len returns the number of connected clients

func (*EventStream) ServeHTTP

func (e *EventStream) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface, requests will be handled if possible. It's this point where your onConnect callback will fire if you set it up.

func (*EventStream) Shutdown

func (e *EventStream) Shutdown()

Shutdown the event stream

type NewOption

type NewOption func(*EventStream)

NewOption for being passed to the New() func

func WithOnConnect

func WithOnConnect(fn func(Emitter)) NewOption

WithOnConnect allows you to pass an OnConnect function that will be passed an Emitter when a new client connects.

type Single

type Single chan<- event

Single is a single event emitter, not a broadcaster like EventStream used to handle on* callbacks

func (Single) Emit

func (c Single) Emit(body interface{}, opts ...EmitOption)

Emit an event

Jump to

Keyboard shortcuts

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