ws

package
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 5 Imported by: 0

README

ws

The WebSocket binding for essessey: a Sink that writes each Event as one WriteJSON frame, and a Source whose Deliver method you wire into a read loop. It never imports a WebSocket library; gorilla's *websocket.Conn already satisfies the one-method Conn interface.

gorilla permits one concurrent writer. The sink locks its own writes, but anything else writing to the same connection needs your own lock.

Full documentation: WebSocket binding.

Documentation

Overview

Package ws provides essessey.Sink and essessey.Source bindings for WebSocket connections.

WebSocket is message-oriented: every write is already a discrete frame, so this package needs no SSE-style framing — that framing exists only because an HTTP body is an undelimited byte stream. Sink sends the whole essessey.Event as one JSON message; Source hands events back exactly as they were delivered.

This package deliberately does NOT import a websocket library such as github.com/gorilla/websocket. Conn declares the one method (WriteJSON(v any) error) this package actually needs, and gorilla's *websocket.Conn already satisfies it structurally — the caller passes their own connection, and essessey adds zero transport dependency. This is a design choice, not a missing feature: pulling in a websocket library would tie essessey's module graph to one driver version for the sake of a single method.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	WriteJSON(v any) error
}

Conn is the minimal capability this package needs from a WebSocket connection: write a value as one JSON-encoded frame.

gorilla's *websocket.Conn (github.com/gorilla/websocket) satisfies this method as-is. Callers pass their own connection — see the package doc comment for why this package never imports a websocket library itself.

type Sink

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

Sink writes essessey events to a WebSocket connection.

WebSocket already delimits frames, so Emit sends the whole essessey.Event as one WriteJSON call — no SSE-style framing. The client receives {"event": ..., "data": ...} on the wire.

Unlike a NATS connection, gorilla's *websocket.Conn permits only one writer at a time, so Emit is mutex-guarded to stay safe for concurrent use.

func NewSink

func NewSink(c Conn) *Sink

NewSink returns a Sink that writes through c.

func (*Sink) Emit

func (s *Sink) Emit(ctx context.Context, ev essessey.Event) error

Emit writes ev to the connection as a single JSON message.

type Source

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

Source turns a WebSocket read loop's per-message delivery into an essessey.Source. Wire Deliver as the callback the read loop invokes per inbound message; Next pulls events off in the order they arrived.

func NewSource

func NewSource() *Source

NewSource returns a ready-to-use Source.

func (*Source) Close

func (s *Source) Close()

Close stops the source. Next drains any already-buffered events first, then returns essessey.ErrNoMoreEvents. Close is idempotent.

func (*Source) Deliver

func (s *Source) Deliver(ev essessey.Event)

Deliver pushes ev onto the queue for Next to pick up. Deliver after Close is a no-op — the source has already stopped accepting events.

func (*Source) Next

func (s *Source) Next(ctx context.Context) (essessey.Event, error)

Next blocks until an event is delivered, the source is closed and drained, or ctx is done.

Jump to

Keyboard shortcuts

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