sse

package
v0.0.0-...-f24e7f3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sse implements functions to send events to a Server-Sent Events (SSE) stream. The format conforms to the ABNF specification here: https://www.w3.org/TR/eventsource/#parsing-an-event-stream.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(h http.Handler) http.Handler

func Send

func Send(w io.Writer, events ...Event) (n int, err error)

Send sends the given events, in order, to a writer, returning the number of bytes written and any error.

If the writer implements http.Flusher, the Flush function is called before Send returns. An extra newline always follows the last event.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/suiteserve/suiteserve/sse"
)

func main() {
	var w bytes.Buffer
	_, err := sse.Send(&w, sse.WithComment("This is a\n comment."),
		sse.WithData("Hello\nthere,\r\n world\r!"),
		sse.WithId("123"))
	if err != nil {
		panic(err)
	}
	_, err = sse.Send(&w, sse.WithRetry(50), sse.WithRetry(100))
	if err != nil {
		panic(err)
	}
	fmt.Println(w.String())
}
Output:

:This is a
: comment.
data:Hello
data:there,
data:  world
data:!
id:123

retry:50
retry:100

func SendWithBom

func SendWithBom(w io.Writer, events ...Event) (n int, err error)

SendWithBom sends a byte-order mark (BOM) followed by the given events, in order, to a writer, returning the number of bytes written and any error. A BOM must only appear at the start of an SSE stream, therefore this function should not be called after a call to Send on the same writer. It is rare to need to use this function at all.

If the writer implements http.Flusher, the Flush function is called before SendWithBom returns. An extra newline always follows the last event.

Types

type Event

type Event func(w io.Writer) (n int, err error)

func WithComment

func WithComment(s string) Event

WithComment returns an Event that writes comment events to the stream.

func WithData

func WithData(s string) Event

WithData returns an Event that writes data events to the stream.

func WithEventType

func WithEventType(s string) Event

WithEventType returns an Event that writes event type events to the stream.

func WithId

func WithId(s string) Event

WithId returns an Event that writes ID events to the stream.

func WithRetry

func WithRetry(i uint64) Event

WithRetry returns an Event that writes retry events to the stream.

Jump to

Keyboard shortcuts

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