websse

package
v0.0.0-...-0d80880 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package main provides an implementation of a Server-Sent Events (SSE) server.

It defines a Server which encapsulates all the logic required to handle SSE connections, register clients for messages, and broadcast messages to registered clients. The server supports multiple event types and ensures thread-safe operations through the use of mutexes.

The package is designed to be integrated into a larger web application, allowing for real-time broadcasting of messages to web clients via an event stream. This is facilitated via EventStreamHandler used by clients to listen to an event stream.

The general flow is:

s := websse.NewServer()
s.RegisterEvents("", "event1", "event2")
s.Run()
...
http.HandleFunc("/event", s.EventStreamHandler)
// client connects to /event?event=event1
...
s.Publish(websse.Message{Event:"event1", Data:"data"})

See example in cmd/simple-websse.

Index

Constants

This section is empty.

Variables

View Source
var ErrEventNotRegistered = errors.New("event not registered")
View Source
var ErrStreamingNotSupported = errors.New("streaming not supported")

Functions

This section is empty.

Types

type Client

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

Client represents event stream clients.

type Message

type Message struct {
	Event string // Event identifies the type (name) of event.
	Data  string // Data is the data field for the message.
	ID    string // ID is the event ID in the client.
	Retry int    // Retry is the reconnection time in milliseconds.
}

Message represents a message in the event stream.

type Server

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

Server encapsulates the server-sent event logic.

func NewServer

func NewServer() *Server

NewServer returns a new server to process server-side events.

func (*Server) EventExists

func (s *Server) EventExists(event string) bool

EventExists returns true if the event exists, otherwise false.

func (*Server) EventStreamHandler

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

EventStreamHandler handles client connections.

The handler waits for messages and sends them to the client. Multiple clients are supported.

The endpoint that accepts an optional "event" query parameter. If the "event" query paramater is not provided, a general message event is assumed per the SSE standard.

func (*Server) Publish

func (s *Server) Publish(msg Message) error

Publish sends a message to the broadcast channel.

func (*Server) RegisterEvent

func (s *Server) RegisterEvent(event string)

RegisterEvent allows the server to accept and respond to event.

func (*Server) RegisterEvents

func (s *Server) RegisterEvents(events ...string)

RegisterEvents allows the server to accept and respond to multiple events.

func (*Server) Run

func (s *Server) Run()

Run runs the server in a goroutine.

func (*Server) SendMessageHandler

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

SendMessageHandler publishes any messages posted to handler.

Accepts query parameters "event", "data", "id", and "retry" to create the message.

Jump to

Keyboard shortcuts

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