Help us shape the future of Go! Take the 2026 Go Developer Survey

sse

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

Go Reference

github.com/bmhatfield/sse

Server sent events publisher in Go

Install

go get github.com/bmhatfield/sse

Usage

Import sse

import "github.com/bmhatfield/sse"

Create the event server. The event server is an async topic publisher, with events pushed to active subscribers.

// Create event server.
events := sse.NewEventServer()

// Create initial topic
events.Create("topic-name")

Create an event, and then broadcast it to subscribers. Messages will be sent to active subscribers. Broadcasts to non-existent topics will fail. Broadcasts to topics without subscribers will be dropped.

// Create an event
// See also: JSONEvent, or implement your own Event!
event := sse.NewEvent("optional-kind", []byte("hi!"))

// Broadcast!
if err := events.Broadcast("topic-name", event); err != nil {
    log.Printf("failed to broadcast: %s", err)
}

The event server is a simple HTTP handler. Use with your favorite router, or serve with the standard library.

// Serve event stream
http.Handle("/events", events)
if err := http.ListenAndServe(":8080", nil); err != nil {
    log.Fatal(err)
}

Use EventSource in js to listen for events. Clients can select their stream with the stream query param.

const stream = new EventSource("/events?stream=points");

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	// Type is optional.
	// Empty values are interpreted by the client as "message" events.
	// Non-empty values require a matching event listener on the client.
	Type() string

	// Data is required. It must conform to UTF-8.
	Data() []byte
}

Event is an interface for our core Event type. Implement on your own types to serialize custom events.

func JSONEvent

func JSONEvent(kind string, data any) (Event, error)

JSONEvent encodes the provided object to JSON and returns as an Event. Kind may be omitted by providing an empty string.

func NewEvent

func NewEvent(kind string, data []byte) Event

NewEvent returns an Event with the provided kind and data. Kind may be omitted by providing an empty string.

type EventServer

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

EventServer is the event server.

func NewEventServer

func NewEventServer() *EventServer

NewEventServer returns an empty EventServer.

func (*EventServer) Broadcast

func (e *EventServer) Broadcast(name string, event Event) error

Broadcast sends an event to all subscribers of the named topic.

func (*EventServer) Create

func (e *EventServer) Create(name string)

Create creates a new topic.

func (*EventServer) ServeHTTP

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

ServeHTTP implements http.Handler to serve events.

func (*EventServer) Stats added in v0.3.0

func (e *EventServer) Stats() Stats

type Stats added in v0.3.0

type Stats struct {
	Topics int

	ByTopic map[string]TopicStats
}

func (Stats) Empty added in v0.4.0

func (s Stats) Empty() bool

type Subscriber

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

Subscriber is an individual topic client.

func NewSubscriber

func NewSubscriber() Subscriber

NewSubscriber returns a new Subscriber.

func (Subscriber) Close

func (s Subscriber) Close()

Close closes the subscriber channel, ending WriteEvents' loop.

func (Subscriber) Push

func (s Subscriber) Push(event Event) error

Push enqueues an event for the subscriber.

func (Subscriber) WriteEvents

func (s Subscriber) WriteEvents(w http.ResponseWriter) error

WriteEvents writes headers and streams events to the provided ResponseWriter.

type SubscriberStats added in v0.3.0

type SubscriberStats struct {
	Pending  int
	Capacity int
}

type Topic

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

Topic is a collection of subscribers.

func NewTopic

func NewTopic(backlog int) *Topic

NewTopic returns a new Topic. The backlog parameter determines how many events are buffered before blocking. The fanout goroutine is started immediately.

func (*Topic) Broadcast

func (t *Topic) Broadcast(event Event) error

Broadcast enqueues an event for all subscribers of this topic.

func (*Topic) Subscribe

func (t *Topic) Subscribe(sub Subscriber)

Subscribe adds a subscriber to this topic.

func (*Topic) Unsubscribe

func (t *Topic) Unsubscribe(ctx context.Context, sub Subscriber)

Unsubscribe removes a subscriber from this topic when the context is cancelled.

type TopicStats added in v0.3.0

type TopicStats struct {
	Subscribers int
	Pending     int
	Capacity    int

	BySubscriber map[string]SubscriberStats
}

type Topics

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

Topics is a collection of topics.

func NewTopics

func NewTopics() *Topics

NewTopics returns an empty set of Topics.

func (*Topics) Create

func (t *Topics) Create(name string) *Topic

Create creates a new topic.

func (*Topics) Stats added in v0.3.0

func (t *Topics) Stats() Stats

Stats compiles a small set of statistics

Jump to

Keyboard shortcuts

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