Documentation
¶
Overview ¶
Package sse provides a server implementation for Server-Sent Events (SSE). SSE is a technology enabling a client to receive automatic updates from a server via HTTP connection.
Index ¶
- Variables
- type Client
- type Error
- type Event
- type EventReader
- type JsonEvent
- type Option
- type PingEvent
- type Server
- func (s *Server) Broadcast(ctx context.Context, event Event) ([]error, error)
- func (s *Server) Get(id string) *Client
- func (s *Server) Join(clientID string, rw http.ResponseWriter) (*Client, int, bool, error)
- func (s *Server) KeepAlive(ctx context.Context)
- func (s *Server) Leave(clientID string, connID int) bool
- func (s *Server) Shutdown()
- type Streamer
- type TextEvent
Constants ¶
This section is empty.
Variables ¶
var ( ErrServerClosed = errors.New("sse: server closed") ErrClientTimeout = errors.New("sse: client timeout") )
var (
ClientTimeout = 10 * time.Second // Default client timeout for SSE connections
)
var ErrNotStreamer = errors.New("sse: not streamer")
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
ID string
// contains filtered or unexported fields
}
Client represents a connection to a streaming service. It holds the client's ID, a Streamer instance for managing the stream, a context for cancellation and timeout, and a channel for signaling closure.
func (*Client) Close ¶
func (c *Client) Close()
Close gracefully shuts down the Client by sending a signal to the close channel. This method should be called to ensure that any ongoing operations are properly terminated.
func (*Client) Send ¶
Send sends an event to the client by writing the event name and data to the response writer. It marshals the event data into JSON format and flushes the output to ensure the data is sent immediately. This method is part of the Client struct and is intended for use in server-sent events (SSE) communication.
type Event ¶
Event represents an interface for writing event data to an io.Writer. Implementations of this interface must provide the Write method, which takes an io.Writer and returns an error if the write operation fails.
type EventReader ¶ added in v1.1.4
type EventReader struct {
// contains filtered or unexported fields
}
func NewReader ¶ added in v1.1.4
func NewReader(r io.ReadCloser) *EventReader
func (*EventReader) Close ¶ added in v1.1.4
func (r *EventReader) Close() error
func (*EventReader) Next ¶ added in v1.1.4
func (r *EventReader) Next() (TextEvent, error)
type JsonEvent ¶
JsonEvent represents an event with a name and associated data. It can be used to structure events in a JSON format in the SSE (Server-Sent Events) protocol.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a structure that manages connected clients in a concurrent environment. It uses a read-write mutex to ensure safe access to the clients map, which holds the active Client instances identified by their unique keys.
func (*Server) Broadcast ¶
Broadcast sends the specified event to all connected clients. It acquires a read lock to ensure thread-safe access to the clients slice, and spawns a goroutine for each client to handle the sending of the event.
func (*Server) Get ¶
Get retrieves the Client associated with the given id from the Server. It uses a read lock to ensure thread-safe access to the clients map. Returns nil if no Client is found for the specified id.
func (*Server) Join ¶
Join adds a new client to the server. It establishes a connection with the specified Streamer and sets the appropriate headers for Server-Sent Events (SSE).
type Streamer ¶
type Streamer interface {
http.ResponseWriter
http.Flusher
}
func NewStreamer ¶
func NewStreamer(rw http.ResponseWriter) (Streamer, error)
NewStreamer creates a new Streamer instance from the provided http.ResponseWriter. It returns an error if the ResponseWriter is nil or does not implement the http.Flusher interface. This function is intended for use in handling server-sent events (SSE).