broadcast

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package broadcast provides a high-level broadcasting system for real-time communication built on top of the WebSocket package and supporting multiple drivers

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnauthorized is returned when channel authorization fails
	ErrUnauthorized = errors.New("unauthorized channel access")

	// ErrDriverNotFound is returned when a driver is not found
	ErrDriverNotFound = errors.New("broadcast driver not found")
)

Functions

func Default

func Default() *broadcaster

Default returns the default broadcaster instance

func New

func New(driver Driver) *broadcaster

New creates a new broadcaster with the given driver

func SetAuthorizer

func SetAuthorizer(fn Authorizer)

SetAuthorizer sets the default authorizer

func SetPresenceData

func SetPresenceData(fn PresenceDataFunc)

SetPresenceData sets the default presence data function

Types

type Authorizer

type Authorizer func(channel string, user interface{}) bool

Authorizer is a function that authorizes channel access

type Broadcaster

type Broadcaster interface {
	// Channel returns a channel builder for broadcasting
	Channel(names ...string) *ChannelBuilder

	// Private returns a private channel builder
	Private(name string) *ChannelBuilder

	// Presence returns a presence channel builder
	Presence(name string) *ChannelBuilder

	// Auth handles channel authorization
	Auth(channel string, socketID string, user interface{}) (interface{}, error)

	// Leave handles user leaving presence channel
	Leave(channel string, socketID string) error
}

Broadcaster defines the main broadcasting interface

type ChannelBuilder

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

ChannelBuilder builds broadcast operations

func Channel

func Channel(names ...string) *ChannelBuilder

Channel returns a channel builder for the given channels

func Presence

func Presence(name string) *ChannelBuilder

Presence returns a presence channel builder

func Private

func Private(name string) *ChannelBuilder

Private returns a private channel builder

func (*ChannelBuilder) Emit

func (cb *ChannelBuilder) Emit(event string, data interface{}) error

Emit broadcasts an event to the channels

func (*ChannelBuilder) ToOthers

func (cb *ChannelBuilder) ToOthers(socketID string) *ChannelBuilder

ToOthers excludes a socket from broadcast

func (*ChannelBuilder) When

func (cb *ChannelBuilder) When(condition bool) *ChannelBuilder

When adds a condition to the broadcast

type Driver

type Driver interface {
	// Broadcast sends an event to channels
	Broadcast(channels []string, event string, data interface{}) error

	// BroadcastExcept broadcasts to all except specified socket
	BroadcastExcept(channels []string, event string, data interface{}, socketID string) error

	// GetClients returns clients in a channel
	GetClients(channel string) []string
}

Driver defines the interface for broadcast drivers

type Event

type Event interface {
	BroadcastOn() []string      // Channels to broadcast on
	BroadcastAs() string        // Event name
	BroadcastWith() interface{} // Data to broadcast
	BroadcastWhen() bool        // Conditional broadcasting
}

Event represents a broadcast event

type PresenceDataFunc

type PresenceDataFunc func(channel string, user interface{}) interface{}

PresenceDataFunc returns presence data for a user

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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