sse

package module
v0.0.0-...-efdcf86 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2018 License: MIT Imports: 5 Imported by: 1

README

go-libsse Go Report CardBuild Status GoDoc

Server-Sent Events for Go

Inspired from alexandrevincenzi's work

About

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

go-libsse is a small, thread-safe library to create a Server-Sent Events server in Go.

Features

  • Fully thread-safe
  • Custom initialization after connection from browser (for example to patch the gap in last-event-id)
  • Custom headers (useful for CORS)
  • Last-Event-ID support (resend lost messages)
  • Follow SSE specification

Getting Started

package main

import (
    "log"
    "net/http"
    "strconv"
    "time"

    "github.com/AdminXVII/go-libsse"
)

func main() {
    // Create the server.
    s := sse.NewServer(nil)

    // Register with /events endpoint.
    http.Handle("/events/", s)

    // Dispatch messages to channel-1.
    go func () {
        for {
            s.SendMessage(sse.Message{Data: time.Now().String()})
            time.Sleep(5 * time.Second)
        }
    }()

    http.ListenAndServe(":3000", nil)
}

Connecting to our server from JavaScript:

e1 = new EventSource('/events/');
e1.onmessage = function(event) {
    // do something...
};

Documentation

Overview

Package sse implements a fully concurrent Server-Sent Events library.

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)

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Id,
	Data,
	Event string
	Retry int
}

Message describes what the server can send

func (*Message) ToBuffer

func (m *Message) ToBuffer() (buffer *bytes.Buffer)

ToBuffer exports the message to a bytes buffer

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
	// All usage logs end up in Logger
	Logger *log.Logger
	// Called when a new client appears. Return a set of messages to send before current messages
	InitMessages func(ClientLastEventId string, ServerLastEventId string) []Message
}

Options contains configuration for the server

type Server

type Server struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Server holds the info for a server

func NewServer

func NewServer(options *Options) *Server

NewServer creates a new SSE server.

func (*Server) GetClientsCount

func (s *Server) GetClientsCount() int

GetClientsCount outputs the current number of active http connections

func (*Server) LastEventId

func (s *Server) LastEventId() string

LastEventId returns the event id of the last event to have been sent

func (*Server) Restart

func (s *Server) Restart()

Restart closes all clients and allow new connections.

func (*Server) SendMessage

func (s *Server) SendMessage(message Message)

SendMessage broadcast a message to all clients

func (*Server) ServeHTTP

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

ServeHTTP is the basic handler for go's http package

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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