push

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CloseSessionInvalid reports that the connection's login session was
	// revoked (logout, concurrent-login eviction, administrative kick) or
	// expired. The client should enter its logged-out flow.
	CloseSessionInvalid = 4401
	// CloseTooManyConnections reports that the per-user connection cap was
	// reached; the client should not retry until another connection closes.
	CloseTooManyConnections = 4429
)

Close codes the push endpoint sends, in the RFC 6455 private-use range (4000-4999). They are part of the client protocol contract: a client seeing one of these must treat the closure as terminal and not auto-reconnect (transport-level failures, by contrast, reconnect with backoff).

Variables

View Source
var (
	// ErrNoTarget reports a Push call with an empty target set or an empty
	// users/roles selector; delivering to nobody is always a caller bug, never
	// a valid broadcast.
	ErrNoTarget = errors.New("push: at least one target is required")
	// ErrTypeRequired reports a message without a type; clients dispatch on it.
	ErrTypeRequired = errors.New("push: message type is required")
	// ErrUnknownTargetKind reports a hand-built target whose kind is not part
	// of the TargetKind vocabulary.
	ErrUnknownTargetKind = errors.New("push: unknown target kind")
)

Functions

This section is empty.

Types

type Message

type Message struct {
	ID      string    `json:"id"`
	Type    string    `json:"type"`
	Payload any       `json:"payload,omitempty"`
	Time    time.Time `json:"time"`
}

Message is the wire envelope every push delivers, serialized as one JSON text frame. Type is the business-defined discriminator clients dispatch on; Payload is an arbitrary JSON-serializable value.

func NewMessage

func NewMessage(messageType string, payload any) Message

NewMessage builds a message with a generated ID and the current time.

type Notifier

type Notifier interface {
	// Push delivers the message to every recipient selected by the targets
	// (their union, each connection at most once). A zero message ID or time
	// is filled in. It returns an error only for an invalid message or target
	// set, never for missed recipients.
	Push(ctx context.Context, message Message, targets ...Target) error
}

Notifier is the business-facing entry of the server push channel. Delivery is best-effort by contract: recipients that are offline, disconnected, or too slow to drain their queue miss the message. Reliable notification belongs in business storage (a notification table the client pulls), with the push acting as the real-time hint.

type Target

type Target struct {
	Kind   TargetKind `json:"kind"`
	Values []string   `json:"values,omitempty"`
}

Target selects push recipients as data, not predicates. Multiple targets on one Push are unioned.

func Broadcast

func Broadcast() Target

Broadcast targets every live connection.

func ToRoles

func ToRoles(roles ...string) Target

ToRoles targets every connection whose principal holds at least one of the given roles (snapshotted at handshake). At least one role is required — Push rejects an empty selector with ErrNoTarget.

func ToUsers

func ToUsers(userIDs ...string) Target

ToUsers targets the live connections of the given user IDs. At least one ID is required — Push rejects an empty selector with ErrNoTarget.

type TargetKind

type TargetKind string

TargetKind enumerates the recipient selectors a push can address.

const (
	// TargetUsers selects the connections of specific user IDs.
	TargetUsers TargetKind = "users"
	// TargetRoles selects the connections whose principal holds any of the roles.
	TargetRoles TargetKind = "roles"
	// TargetBroadcast selects every live connection.
	TargetBroadcast TargetKind = "broadcast"
)

Jump to

Keyboard shortcuts

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