Documentation ¶
Overview ¶
Package sseserver implements a reference Server-Sent Events server, suitable for streaming unidirectional messages over HTTP to web browsers.
This implementation also adds easy namespacing so that clients can subscribe to only a specific subset of messages.
Server-Sent Events ¶
For more information on the SSE format itself, check out this fairly comprehensive article: http://www.html5rocks.com/en/tutorials/eventsource/basics/
Note that the implementation of SSE in this server intentionally does not implement message IDs.
Namespacing ¶
The server opens a HTTP endpoint at /subscribe/:namespace that will accept connections on any suitable child path. The remainder of the path is a virtual endpoint represents the "namespace" for the client connection. For example:
HTTP GET /subscribe/pets // client subscribed to "/pets" namespace HTTP GET /subscribe/pets/cats // client subscribed to "/pets/cats" namespace HTTP GET /subscribe/pets/dogs // client subscribed to "/pets/dogs" namespace
SSEMessages broadcast via the server are only delivered to clients subscribed to the endpoint matching their namespace. Namespaces are hierarchical, subscribing to the "parent" endpoint would receive all messages broadcast to all child namespaces as well. E.g. in the previous example, a subscription to "/pets" would receive all messages broadcast to both the dogs and cats namespaces as well.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ProxyRemoteAddrHandler ¶
ProxyRemoteAddrHandler is HTTP middleware to determine the actual RemoteAddr of a http.Request when your server sits behind a proxy or load balancer.
When utilized, the value of RemoteAddr will be overridden based on the X-Real-IP or X-Forwarded-For HTTP header, which can be a comma separated list of IPs.
See http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#x-headers for details.
Based on http://git.io/xDD3Mw
Types ¶
type ReportingStatus ¶
type ReportingStatus struct { Node string `json:"node"` Status string `json:"status"` Reported int64 `json:"reported_at"` StartupTime int64 `json:"startup_time"` SentMsgs uint64 `json:"msgs_broadcast"` Connections connStatusList `json:"connections"` }
ReportingStatus is snapshot of metadata about the status of a Server
It can be serialized to JSON and is what gets reported to admin API endpoint.
type SSEMessage ¶
type SSEMessage struct { Event string // event scope for the message [optional] Data []byte // message payload Namespace string // namespace for msg, matches to client subscriptions }
SSEMessage is a message suitable for sending over a Server-Sent Event stream.
Note: Namespace is not part of the SSE spec, it is merely used internally to map a message to the appropriate HTTP virtual endpoint.
type Server ¶
type Server struct { Broadcast chan<- SSEMessage Options ServerOptions // contains filtered or unexported fields }
Server is the primary interface to a SSE server.
Exposes a receive-only chan `Broadcast`: any SSEMessage sent to this channel will be broadcast out to any connected clients subscribed to a namespace that matches the message.
Server implements the http.Handler interface, and can be chained into existing HTTP routing muxes if desired.
func NewServer ¶
func NewServer() *Server
NewServer creates a new Server and returns a reference to it.
func (*Server) Serve ¶
Serve is a convenience method to begin serving connections on specified address.
This method blocks forever, as it is basically a convenience wrapper around http.ListenAndServe(addr, self).
It also implements basic request logging to STDOUT.
If you want to do something more sophisticated, you should not use this method, but rather just build your own HTTP routing/middleware chain around Server which implements the standard http.Handler interface.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface
func (*Server) Status ¶
func (s *Server) Status() ReportingStatus
Status returns the ReportingStatus for a given server.
Primarily intended for logging and reporting.
type ServerOptions ¶
type ServerOptions struct {
DisableAdminEndpoints bool // disables the "/admin" status endpoints
}
ServerOptions defines a set of high-level user options that can be customized for a Server.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
petstore
In this example we will still only send simple text messages, but one can just as easily serialize objects to JSON and send them through the data field, and deserialize back into Javascript objects on the client side.
|
In this example we will still only send simple text messages, but one can just as easily serialize objects to JSON and send them through the data field, and deserialize back into Javascript objects on the client side. |