ws

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ws provides a WebSocket transport for tether. WebSocket gives full-duplex communication over a single TCP connection, so both server updates and client events travel on the same channel with minimal overhead. This is the default and preferred transport.

Pass ws.Upgrade() as the Upgrade field in [tether.StatefulConfig]. Origin checking is handled by the tether handler via [tether.StatefulConfig].TrustedOrigins rather than by the websocket library directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Upgrade

func Upgrade(opts ...Options) func(http.ResponseWriter, *http.Request) (xport.Transport, error)

Upgrade returns an upgrade function for use in [tether.StatefulConfig].Upgrade. The returned function is called by the tether handler when it receives a WebSocket upgrade request. It negotiates the WebSocket handshake and returns a Transport that the session uses for its entire lifetime.

Origin checking is handled by the tether handler via [tether.StatefulConfig].TrustedOrigins, so the upgrader does not perform its own origin verification.

Types

type Compression

type Compression struct {
	// Disabled turns off per-message deflate. Use this when a
	// reverse proxy already handles compression, or for debugging.
	Disabled bool

	// Level sets the deflate compression level. Zero defaults to
	// [CompressionFastest], which is the best trade-off for
	// real-time updates.
	Level CompressionLevel

	// Threshold is the minimum message size in bytes before
	// compression is applied. Messages smaller than this are sent
	// uncompressed to avoid the overhead of deflating tiny
	// payloads. Zero defaults to 512 bytes.
	Threshold int

	// ContextTakeover enables per-connection compression context.
	// The compressor retains its sliding window across messages,
	// producing significantly better ratios for repetitive content
	// like HTML fragments. The cost is additional memory per
	// connection (~4 KB at the default window size) instead of a
	// fixed shared pool. When disabled (default), each message is
	// compressed independently using a shared pool of compressors.
	ContextTakeover bool
}

Compression configures WebSocket per-message deflate (RFC 7692). Compression is enabled by default with sensible defaults - set Disabled to opt out. When enabled, the server negotiates the permessage-deflate extension during the WebSocket handshake. Browsers handle decompression transparently.

type CompressionLevel

type CompressionLevel int

CompressionLevel controls the deflate compression level for per-message compression. Higher levels produce smaller messages at the cost of more CPU. The zero value selects the default (fastest).

const (
	// CompressionFastest uses the least CPU per message. This is
	// the default and the best choice for real-time updates where
	// latency matters more than payload size.
	CompressionFastest CompressionLevel = 1

	// CompressionBalanced trades some CPU for better compression
	// ratios. Useful when bandwidth is more constrained than CPU.
	CompressionBalanced CompressionLevel = 6

	// CompressionSmallest produces the smallest possible messages
	// at the highest CPU cost. Rarely appropriate for real-time
	// traffic.
	CompressionSmallest CompressionLevel = 9
)

type Options

type Options struct {
	// ReadLimit sets the maximum message size in bytes that the server
	// will accept from a client. Messages exceeding this limit cause
	// the connection to be closed with a protocol error. When zero,
	// the library default is used. Set this to match
	// [tether.StatefulConfig].MaxEventBytes for consistent limits across
	// transport modes.
	ReadLimit int64

	// Compression configures per-message deflate (RFC 7692).
	// The zero value enables compression with sensible defaults:
	// fastest compression level, 512-byte threshold, and a shared
	// compressor pool. Set Compression.Disabled to opt out.
	Compression Compression
}

Options configures the WebSocket transport.

Jump to

Keyboard shortcuts

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