sse

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: MIT Imports: 6 Imported by: 0

README

SSE - Simple and Compliant Server-Sent Events Parser for Go

Go Report Card GoDoc

Keywords: Golang SSE, Server-Sent Events, SSE Reader, SSE Parser, Go SSE Client

Overview

sse is a lightweight, fully spec compliant Server-Sent Events (SSE) reader library for Go. It provides a simple wrapper over an io.Reader (typically an HTTP response body) so you can easily handle streaming SSE events in your Go applications. The design is really simple, easy to use, and perfect for developers looking to implement real-time event handling in a minimalistic way.

Installation

go get github.com/dblokhin/sse

Usage Example

sse.Read returns iterator, so it is a straightforward way to handle SSE stream data from an io.Reader source:

	// ... 
	// Use the sse.Read iterator directly in a range loop.
	for event, err := range sse.Read(resp.Body) {
		if err != nil {
			// error handling ...
		}
		
		// process the valid event.
		fmt.Println("received event:", event)
	}
}

Error Handling

Any invalid SSE line sequence is returned as an error (ErrInvalidSequence) with detailed message content, allowing graceful handling or logging of issues.


License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSequence = errors.New("invalid event sequence")
)

Functions

func Read

func Read(input io.Reader) iter.Seq2[Event, error]

Read returns iterator over streaming in input reader. It automatically skips comments. Returns ErrInvalidSequence with the problem line if got invalid line sequence.

Types

type Event

type Event struct {
	ID    []byte // The event ID to set the EventSource object's last event ID value.
	Event []byte // A string identifying the type of event described.
	Data  []byte // The data field for the message.
	Retry []byte // The reconnection time (milliseconds). If a non-integer value is specified, the field is ignored.
}

Event is message unit in SSE

Jump to

Keyboard shortcuts

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