sse

package
v0.0.0-...-25a5272 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2022 License: Unlicense Imports: 4 Imported by: 0

Documentation

Overview

Package sse implements Server Sent Event API for szmaterlok backend server. You'll find here both type models and http methods and functions.

Index

Constants

View Source
const ContentTypeEventStream string = "text/event-stream"

ContentTypeEventStream is content type for event stream filetype.

Variables

This section is empty.

Functions

func Encode

func Encode(stream io.Writer, v Event) error

Encode writes the event stream encoding of v to the stream, followed by a newline character.

func Headers

func Headers(next http.Handler) http.Handler

Headers is middleware that sets up http headers for SSE http handler.

Types

type Event

type Event struct {
	// Type is a string identifying the type of event described. If
	// this is specified, an event will be dispatched on the browser
	// to the listener for the specified event name; the website source
	// code should use addEventListener() to listen for named events.
	Type string

	// Data is the field for the message.
	Data []byte

	// ID is unique event identifier.
	ID string

	// Retry is reconnection time. If the connection to the server is lost, the
	// browser will wait for the specified time before attempting to reconnect.
	// This must be an integer, specifying the reconnection time in milliseconds.
	// If a non-integer value is specified, the field is ignored.
	Retry int64
}

Event is a simple stream of text data which must be encoded using UTF-8. Messages in the event stream are separated by a pair of newline characters. A colon as the first character of a line is in essence a comment, and is ignored.

Sample event stream.

event: usermessage
data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}

Below and above documentation is mostly copied from MDN. Source: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

func (Event) Stream

func (e Event) Stream() ([]byte, error)

Stream encodes Event into text/event-stream format and returns it as slice of bytes.

Jump to

Keyboard shortcuts

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