sse

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2019 License: MIT Imports: 6 Imported by: 4

README

Server Sent Events for Go Last release Documentation

Go Report Card

Branch Status Coverage
master Build Status Coveralls

Golang Server Sent Events server

Example

Server
package main

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

	"github.com/euskadi31/go-sse"
)

func main() {
	serve := sse.NewServer(func(rw sse.ResponseWriter, r *http.Request) {
		tickChan := time.NewTicker(time.Second * 2).C

		// recovery
		lastID := r.Header.Get(sse.LastEventID)
		if lastID != "" {
			log.Printf("Recovery with ID: %s\n", lastID)
		}

		for {
			select {
			case t := <-tickChan:
				eventString := fmt.Sprintf("the time is %v", t)

				log.Println("Send event...")

				rw.Send(&sse.MessageEvent{
					ID:   strconv.Itoa(int(t.Unix())),
					Data: []byte(eventString),
				})
			case <-r.Context().Done():
				log.Println("Done")

				return
			}
		}
	})

	serve.SetRetry(time.Second * 5)

	http.Handle("/events", serve)

	log.Panic(http.ListenAndServe(":1337", nil))
}

Client
var client = new EventSource("http://localhost:1337/events");

client.onmessage = (msg) => {
    console.log(msg);
};

License

go-sse is licensed under the MIT license.

Documentation

Index

Constants

View Source
const LastEventID = "Last-Event-ID"

LastEventID header

Variables

View Source
var ErrDurationEmpty = errors.New("duration is empty")

ErrDurationEmpty message

View Source
var ErrMessageEmpty = errors.New("message is empty")

ErrMessageEmpty message

Functions

This section is empty.

Types

type EventMarshaler

type EventMarshaler interface {
	MarshalEvent() ([]byte, error)
}

EventMarshaler interface

type HandlerFunc

type HandlerFunc func(ResponseWriter, *http.Request)

HandlerFunc type

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *http.Request)

ServeHTTP calls f(w, r).

type MessageEvent

type MessageEvent struct {
	ID    string
	Event string
	Data  []byte
}

MessageEvent struct

func (MessageEvent) MarshalEvent

func (m MessageEvent) MarshalEvent() ([]byte, error)

MarshalEvent implements the encoding.TextMarshaler interface.

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriter struct

func (*ResponseWriter) Send

func (rw *ResponseWriter) Send(data EventMarshaler)

Send data to client

type Retry

type Retry struct {
	Duration time.Duration
}

Retry struct

func (Retry) MarshalEvent

func (r Retry) MarshalEvent() ([]byte, error)

MarshalEvent implements the EventMarshaler interface.

type Server

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

Server struct

func NewServer

func NewServer(handle HandlerFunc) *Server

NewServer constructor

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request)

func (*Server) SetRetry

func (s *Server) SetRetry(duration time.Duration)

SetRetry duration

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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