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 ¶
- Variables
- type Client
- type Message
- type Server
- func (s *Server) EventExists(event string) bool
- func (s *Server) EventStreamHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) Publish(msg Message) error
- func (s *Server) RegisterEvent(event string)
- func (s *Server) RegisterEvents(events ...string)
- func (s *Server) Run()
- func (s *Server) SendMessageHandler(w http.ResponseWriter, r *http.Request)
Constants ¶
This section is empty.
Variables ¶
var ErrEventNotRegistered = errors.New("event not registered")
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 ¶
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) RegisterEvent ¶
RegisterEvent allows the server to accept and respond to event.
func (*Server) RegisterEvents ¶
RegisterEvents allows the server to accept and respond to multiple events.
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.