sse

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

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

Go to latest
Published: Aug 22, 2024 License: MIT Imports: 5 Imported by: 0

README

go-sse

PkgGoDev

Package sse implements a Server-Sent Events server.

In short, it makes sending events to a client just like writing to a Go channel.

Example

sseServer := sse.NewServer(
	sse.WithConnectFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool {
		w.Header().Set("Access-Control-Allow-Origin", "*")
		return true
	}),
	sse.WithEstablishedFunc(func(ctx context.Context, ch chan<- sse.Event, r *http.Request) {
		defer close(ch)
		for i := 0; i < 4; i++ {
			time.Sleep(time.Second)
			ch <- sse.Event{
				Name: fmt.Sprintf("event-%d", i),
				Data: []byte(strconv.Itoa(i)),
			}
		}
	}),
)

serverMux := http.NewServeMux()
serverMux.Handle("GET /events", sseServer)

http.ListenAndServe(":8080", serverMux)

License

MIT

Documentation

Overview

Package sse implements a Server-Sent Events server.

See https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultChannelSizeFunc

func DefaultChannelSizeFunc(ctx context.Context, r *http.Request) int

DefaultChannelSizeFunc is the default channel size function. It always returns a small non-zero constant.

func DefaultConnectFunc

func DefaultConnectFunc(ctx context.Context, w http.ResponseWriter, r *http.Request) bool

DefaultConnectFunc is the default connect function. It always returns true.

func DefaultErrorFunc

func DefaultErrorFunc(ctx context.Context, err error, r *http.Request) error

DefaultErrorFunc is the default error function. It returns err unchanged, i.e. it terminates the connection as soon as any error is encountered.

func DefaultEstablishedFunc

func DefaultEstablishedFunc(ctx context.Context, ch chan<- Event, r *http.Request)

DefaultEstablishedFunc is the default established function. It immediately closes ch, i.e. it terminates the connection immediately.

Types

type ChannelSizeFunc

type ChannelSizeFunc func(ctx context.Context, r *http.Request) int

A ChannelSizeFunc is called when a connection is established. It returns the channel size for the client.

type ConnectFunc

type ConnectFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool

A ConnectFunc is called when a client connects. If it returns true then the connection is established. If it returns false then it should write the response to w.

type ErrorFunc

type ErrorFunc func(ctx context.Context, err error, r *http.Request) error

A ErrorFunc is called when an error is encountered while sending an event. If it returns non-nil then the connection to the client is terminated.

type EstablishedFunc

type EstablishedFunc func(ctx context.Context, ch chan<- Event, r *http.Request)

An EstablishedFunc is called when a connection with the client is established. It receives a channel to which it should write events to be sent to the client. The function must close ch before returning and terminate when r's context is done.

type Event

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

An Event is a server-sent event.

func (*Event) MarshalText

func (e *Event) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.MarshalText.

func (*Event) Write

func (e *Event) Write(data []byte) (int, error)

type Server

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

A Server is a Server-Sent Events Server.

func NewServer

func NewServer(options ...ServerOption) *Server

NewServer returns a new Server with the given options.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements net/http.Handler.ServeHTTP.

type ServerOption

type ServerOption func(*Server)

A ServerOption sets an option on a Server.

func WithChannelSizeFunc

func WithChannelSizeFunc(channelSizeFunc ChannelSizeFunc) ServerOption

WithChannelSizeFunc sets the channel size function.

func WithConnectFunc

func WithConnectFunc(connectFunc ConnectFunc) ServerOption

WithConnectFunc sets the connection function.

func WithErrorFunc

func WithErrorFunc(errorFunc ErrorFunc) ServerOption

WithErrorFunc sets the error function.

func WithEstablishedFunc

func WithEstablishedFunc(establishedFunc EstablishedFunc) ServerOption

WithEstablishedFunc sets the established function.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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