sse

package module
v0.0.0-...-5e724d4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2018 License: MIT Imports: 7 Imported by: 0

README

sse Build Status Coverage Status

This package provides several tools for working with Server Sent Event streams in Go, including a client library, stream parser, and stream generator.

Client

To subscribe to an SSE stream, pass a http.Request object to client.Subscribe:

package main

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

	"github.com/btubbs/sse"
)

func main() {
	req, err := http.NewRequest(
		http.MethodGet,
		"https://infinite-mountain-77592.herokuapp.com/events/",
		nil,
	)
	if err != nil {
		panic(err.Error())
	}

	client := sse.Client{}
	log.Fatal(client.Subscribe(req, func(e sse.Event) {
		fmt.Println(
			client.LastID,
			string(e.Data),
		)
	}))
}

TODO:

  • Add automatic retry logic to client
  • Add event emitter/publisher that will periodically push a comment line to the stream to act as a keepalive.
  • set headers
  • add Disconnect method to client
  • Add examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(r io.Reader, f func(Event))

Parse takes a reader and a callback function. It reads the stream and parses out SSE event payloads, builds an Event struct for each, and passes it into the provided callback.

func Subscribe

func Subscribe(req *http.Request, f func(Event), options ...ClientOption) error

Subscribe is a shortcut for instantiating your own Client and calling its .Subscribe method.

Types

type Client

type Client struct {
	http.Client
	RetryDuration time.Duration
	LastID        string
}

Client works like a regular http.Client, with an additional Subscribe method for connecting to Server Sent Event streams.

func (*Client) Subscribe

func (c *Client) Subscribe(req *http.Request, f func(Event), options ...ClientOption) error

Subscribe performs the provided request, then passes the response into the SSE parser, calling the provided callback with each Event parsed from the stream.

type ClientOption

type ClientOption func(*clientOptions)

A ClientOption is a function that can be passed as an argument to Client.Subscribe to alter its behavior.

func AutoRetry

func AutoRetry(retry bool) ClientOption

AutoRetry takes a bool that flags whether the client should automatically reconnect on errors. The ClientOption that you get back should be passed into Client.Subscribe.

func LastEventID

func LastEventID(id string) ClientOption

LastEventID takes the ID that should be passed to the server to start the stream where you left off. The ClientOption that you get back should be passed into Client.Subscribe.

type Event

type Event struct {
	ID    string
	Event string
	Retry time.Duration
	Data  []byte
}

func (Event) Bytes

func (e Event) Bytes() []byte

Bytes serializes the Event to a byte slice suitable for writing out to the wire as a valid SSE event.

type EventWriter

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

EventWriter is a helper for writing events to an http.ResponseWriter stream.

func NewEventWriter

func NewEventWriter(w http.ResponseWriter) *EventWriter

NewEventWriter returns a new EventWriter.

func (*EventWriter) Write

func (evw *EventWriter) Write(e Event) error

Write takes an Event, serializes the Event to bytes, and writes them out to the http stream. If the stream is also an http.Flusher, then it will also flush the bytes out so the event will be sent over the wire immediately. It automatically sets the Content-Type header to text/event-stream.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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