sse

package
v0.1.29 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package sse holds the server-sent-events wire format shared by every stream this server serves — session events and the devtools console alike.

It is deliberately only the wire: framing, the resume header, and the flush-and-keepalive contract. What to stream, and when a stream is over, is the caller's business.

Index

Constants

View Source
const DefaultKeepalive = 20 * time.Second

DefaultKeepalive is how often an otherwise silent stream reassures the proxies between here and the browser that the connection is alive. A trace that ran for seconds never needed one; a tail can sit idle for minutes, and the common idle timeout in front of it is a minute.

View Source
const KeepaliveFrame = ": ping\n\n"

KeepaliveFrame is a comment frame: the one thing that can be written into an event stream without being an event.

Write it from the same goroutine — ideally the same select — that writes event frames. A ping emitted from a timer goroutine can land between a frame's id and its data, which splits the frame and desynchronises the client for the life of the connection.

Variables

This section is empty.

Functions

func Begin

Begin writes the event-stream headers and returns the flusher the stream must call after every frame.

It panics rather than degrading when the writer cannot flush: an unflushed event stream looks like a hung request, which is far harder to diagnose than a stack trace naming the middleware that swallowed the Flusher.

func LastEventSequence

func LastEventSequence(r *http.Request) (int64, error)

LastEventSequence reads the resume header a browser sends after a dropped connection. Anything that is not a sequence is a client the server cannot answer correctly — replaying the whole ring instead would silently duplicate every event it already has.

func WriteFrame

func WriteFrame(w io.Writer, frame Frame) error

WriteFrame writes one frame; an error means the client disconnected and the stream should stop.

func WriteNDJSON

func WriteNDJSON[T any](w http.ResponseWriter, filename string, items []T)

WriteNDJSON serves the same events as a downloadable file, for a caller that wants the stream's contents without following it.

Types

type Frame

type Frame struct {
	Event string
	ID    int64
	Data  any
}

Frame is one wire frame. ID carries the event's sequence, which is what a reconnecting browser echoes back in Last-Event-ID — a frame without it is a frame the client cannot resume from, so only a terminal frame (which nobody resumes into) leaves it unset.

Jump to

Keyboard shortcuts

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