sse

package module
v0.0.0-...-875881e Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2018 License: MIT Imports: 7 Imported by: 1

README

SSE Announce Server

GoDoc Build Status Maintainability Test Coverage

Go server for announcing a stream of Server-Sent Events.

Example

An SSE announce server creates an http.Handler that can be registered to an existing router in your application. It takes a read-only channel of interface objects to send to all connected clients. Once a client connects they will begin receiving all events that occur at that point until they disconnect. Closing the event channel will shutdown the server's background routines.

events := make(chan interface{})
server := NewServer(events, WithBufferSize(50))

go func() {
    if err := server.Start(); err != nil {
        panic(err.Error())
    }
}()

go func() {
    defer close(events)

    events <- map[string]int{"foo": 1}
    events <- map[string]int{"bar": 2}
    events <- map[string]int{"baz": 3}
}()

http.Handle("/events", server.ServeHTTP)
http.ListenAndServe("0.0.0.0:8080", nil)

License

Copyright (c) 2017 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigFunc

type ConfigFunc func(*Server)

ConfigFunc is a function used to initialize a new server.

func WithBufferSize

func WithBufferSize(bufferSize int) ConfigFunc

WithBufferSize sets the internal buffer for each connected client. This buffer counts distinct events. The default is 100.

type Server

type Server struct {
	ServeHTTP http.HandlerFunc
	// contains filtered or unexported fields
}

Server is an http.Handler that will fanout Server-Sent Events to all connected clients.

func NewServer

func NewServer(events <-chan interface{}, configs ...ConfigFunc) *Server

NewServer creates a new server with the given event channel. The server returned by this function has not yet started.

func (*Server) Handler

func (s *Server) Handler(r *http.Request) response.Response

Handler converts an HTTP request into a streaming response. This can be used with libraries that utilize efritz/response. Alternatively, the ServeHTTP member on the Server struct is a http.HandlerFunc that can be served directly.

func (*Server) Start

func (s *Server) Start() error

Start will begin serializing events that come in on the event channel and sending the payload to each registered client. This method will block until the event channel has closed.

Jump to

Keyboard shortcuts

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