package sse

import "github.com/fenole/szmaterlok/service/sse"

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

const ContentTypeEventStream string = "text/event-stream"

ContentTypeEventStream is content type for event stream filetype.

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.

Source Files

sse.go

Version
v0.0.0-20220430131856-25a527277f06 (latest)
Published
Apr 30, 2022
Platform
linux/amd64
Imports
4 packages
Last checked
3 weeks ago

Tools for package owners.