Help us shape the future of Go! Take the 2026 Go Developer Survey

sse

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

README

server/sse

Import path: github.com/InsideGallery/core/server/sse

sse provides basic Server-Sent Events formatting, streaming, and per-user connection pooling for net/http handlers.

Main APIs

  • Message and NewMessage(event, data...): event payload model.
  • FormatMSG(event, data...): renders SSE bytes using event: and data: lines.
  • Run(ch, w, r): streams messages from a channel to an http.ResponseWriter.
  • Pool: tracks user ID to message channel mappings.
  • NewPool(bufferSize): creates a pool; non-positive sizes use the default.
  • Pool.Add, Remove, Send, SendToAll, StopAll, Connections, and GetAllConnectedUsers: connection lifecycle and delivery helpers.
  • Pool.Handler: HTTP handler that reads ContextUserID from request context.

Usage

pool := sse.NewPool(100)

http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
	ctx := context.WithValue(r.Context(), sse.ContextUserID, "user-1")
	pool.Handler(w, r.WithContext(ctx))
})

_ = pool.Send("user-1", sse.NewMessage("notice", "hello"))

Operational Notes

Run requires a response writer that implements http.Flusher; otherwise it returns ErrResponseWriterIsNotFlusher. It sets keep-alive, text/event-stream, and wildcard CORS headers. Pool.Handler logs and returns without a connection when ContextUserID is missing or not a string.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidUserID              = errors.New("invalid user id")
	ErrNotFoundConnectedUser      = errors.New("not found connected user")
	ErrResponseWriterIsNotFlusher = errors.New("response does not support flush")
)

All kind of errors

View Source
var (
	ContextUserID strings.ContextKey = "userID"
)

Describe all context keys

Functions

func FormatMSG

func FormatMSG(event string, data ...string) []byte

FormatMSG prepare bytes to send

func Run

func Run(ch chan Message, w http.ResponseWriter, r *http.Request) error

Run listener of channel and prepare messages

Types

type Message

type Message struct {
	Event string
	Text  []string
}

Message message

func NewMessage

func NewMessage(event string, data ...string) Message

NewMessage return new message

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool contains all connections and could delivery messages

func NewPool

func NewPool(bufferSize int) *Pool

NewPool return new handler

func (*Pool) Add

func (h *Pool) Add(userID string) chan Message

Add add new connection

func (*Pool) Connections

func (h *Pool) Connections() int

Connections return count of connections

func (*Pool) GetAllConnectedUsers

func (h *Pool) GetAllConnectedUsers() set.GenericDataSet[string]

GetAllConnectedUsers get all connected users

func (*Pool) Handler

func (h *Pool) Handler(w http.ResponseWriter, r *http.Request)

Handler listen for messages

func (*Pool) Remove

func (h *Pool) Remove(userID string)

Remove remove connection

func (*Pool) Send

func (h *Pool) Send(userID string, msg Message) error

Send send message to all connections

func (*Pool) SendToAll

func (h *Pool) SendToAll(msg Message)

SendToAll send message to all connections

func (*Pool) StopAll

func (h *Pool) StopAll()

StopAll stop all connections

Jump to

Keyboard shortcuts

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