Back to godoc.org
github.com/alexandrevicenzi/go-sse

Package sse

v1.3.1-0...-7b23d5f
Latest Go to latest

The latest major version is .

Published: Jan 17, 2020 | License: MIT | Module: github.com/alexandrevicenzi/go-sse

Overview

Package sse implements Server-Sent Events that supports multiple channels.

Server-sent events is a method of continuously sending data from a server to the browser, rather than repeatedly requesting it.

Examples

Basic usage of sse package.

s := sse.NewServer(nil)
defer s.Shutdown()

http.Handle("/events/", s)

Index

type Channel

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

Channel represents a server sent events channel.

func (*Channel) ClientCount

func (c *Channel) ClientCount() int

ClientCount returns the number of clients connected to this channel.

func (*Channel) Close

func (c *Channel) Close()

Close closes the channel and disconnect all clients.

func (*Channel) LastEventID

func (c *Channel) LastEventID() string

LastEventID returns the ID of the last message sent.

func (*Channel) SendMessage

func (c *Channel) SendMessage(message *Message)

SendMessage broadcast a message to all clients in a channel.

type Client

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

Client represents a web browser connection.

func (*Client) Channel

func (c *Client) Channel() string

Channel returns the channel where this client is subscribe to.

func (*Client) LastEventID

func (c *Client) LastEventID() string

LastEventID returns the ID of the last message sent.

func (*Client) SendMessage

func (c *Client) SendMessage(message *Message)

SendMessage sends a message to client.

type Message

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

Message represents a event source message.

func NewMessage

func NewMessage(id, data, event string) *Message

func SimpleMessage

func SimpleMessage(data string) *Message

func (*Message) String

func (m *Message) String() string

type Options

type Options struct {
	// RetryInterval change EventSource default retry interval (milliseconds).
	RetryInterval int
	// Headers allow to set custom headers (useful for CORS support).
	Headers map[string]string
	// ChannelNameFunc allow to create custom channel names.
	// Default channel name is the request path.
	ChannelNameFunc func(*http.Request) string
	// All usage logs end up in Logger
	Logger *log.Logger
}

Options holds server configurations.

type Server

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

Server represents a server sent events server.

func NewServer

func NewServer(options *Options) *Server

NewServer creates a new SSE server.

func (*Server) Channels

func (s *Server) Channels() []string

Channels returns a list of all channels to the server.

func (*Server) ClientCount

func (s *Server) ClientCount() int

ClientCount returns the number of clients connected to this server.

func (*Server) CloseChannel

func (s *Server) CloseChannel(name string)

CloseChannel closes a channel.

func (*Server) GetChannel

func (s *Server) GetChannel(name string) (*Channel, bool)

GetChannel returns the channel associated with name or nil if not found.

func (*Server) HasChannel

func (s *Server) HasChannel(name string) bool

HasChannel returns true if the channel associated with name exists.

func (*Server) Restart

func (s *Server) Restart()

Restart closes all channels and clients and allow new connections.

func (*Server) SendMessage

func (s *Server) SendMessage(channelName string, message *Message)

SendMessage broadcast a message to all clients in a channel. If channelName is an empty string, it will broadcast the message to all channels.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(response http.ResponseWriter, request *http.Request)

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown performs a graceful server shutdown.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier