sse

package module
v0.0.0-...-662c9e3 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2016 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Go library for SSE (Server-Sent Events) client

GoDoc

Implementation of Server-Sent Events EventSource interface. Still work in progress - API might break!

Simple usage:

package main

import (
	"fmt"
	"log"
	"runtime"

	"github.com/rndz/sse"
)

func main() {
	cfg := &sse.Config{
		URL: "http://example.com/chat",
	}
	client, err := sse.New(cfg)
	if err != nil {
		log.Fatal(err)
	}
	client.AddListener("join", func(e sse.Event) {
		fmt.Printf("%s has joined the conversation\n", e.Data)
	})
	client.AddListener("leave", func(e sse.Event) {
		fmt.Printf("%s has left the conversation\n", e.Data)
	})
	client.AddListener("error", func(e sse.Event) {
		log.Printf("error in sse stream: %s", e.Data)
	})
	client.Connect()
	runtime.Goexit()
}

Documentation

Overview

Example
package main

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

	"github.com/rndz/sse"
)

func main() {
	s := &Server{}
	go runClient()
	log.Fatal("HTTP server error: ", http.ListenAndServe("localhost:3000", s))
}

type Server struct {
}

func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	flusher, ok := rw.(http.Flusher)
	if !ok {
		http.Error(rw, "Streaming unsupported!", http.StatusInternalServerError)
		return
	}

	rw.Header().Set("Content-Type", "text/event-stream")
	rw.Header().Set("Cache-Control", "no-cache")
	rw.Header().Set("Connection", "keep-alive")
	rw.Header().Set("Access-Control-Allow-Origin", "*")
	for {
		fmt.Fprintf(rw, "event: ts\ndata: %s\n\n", time.Now())
		fmt.Fprintf(rw, "event: new\ndata: %s\n\n", time.Now())
		flusher.Flush()
		time.Sleep(time.Second * 2)
		return
	}
}

func runClient() {
	time.Sleep(1 * time.Second)
	cfg := &sse.Config{
		URL: "http://localhost:3000",
	}
	client, err := sse.New(cfg)
	if err != nil {
		log.Fatal(err)
	}
	client.AddListener("ts", func(e sse.Event) {
		fmt.Printf("%+v\n", e)
	})
	client.AddListener("error", func(e sse.Event) {
		fmt.Println("error ", e)
	})
	client.AddListener("open", func(e sse.Event) {
		fmt.Println("open ", e)
	})
	client.Connect()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is main struct that handles connecting/streaming/etc...

func New

func New(cfg *Config) (*Client, error)

New creates a client based on a passed Config.

func (*Client) AddListener

func (c *Client) AddListener(event string, f func(Event))

AddListener adds a listener for a given event type. A listener is simple callback function that passes Event struct.

func (*Client) Connect

func (c *Client) Connect()

Connect connects to given SSE endpoint and starts reading the stream and transmitting events.

func (*Client) Stop

func (c *Client) Stop()

Stop stops the client of accepting any more stream requests.

type Config

type Config struct {
	Client      *http.Client
	URL         string
	Retry       time.Duration
	LastEventID string
}

Config is a struct used to define and override default parameters:

Client      - *http.Client, if non provided, http.DefaultClient will be used.
URL         - URL of SSE stream to connect to. Must be provided. No default
              value.
Retry       - time.Duration of how long should SSE client wait before trying
              to reconnect after disconnection. Default is 2 seconds.
LastEventID - set LastEventID for the first requests.

type Event

type Event struct {
	ID    string
	Event string
	Data  string
}

Event is SSE event data represenation

Jump to

Keyboard shortcuts

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