Documentation
¶
Overview ¶
Package websocket provides WebSocket infrastructure for real-time communication.
Index ¶
- func MakeChannel(channelType ChannelType, id string) string
- type AuthorizeFunc
- type BridgeConfig
- type BroadcastMessage
- type BroadcastPublisher
- type ChannelType
- type Client
- func (c *Client) Close()
- func (c *Client) GetSubscriptions() []string
- func (c *Client) IsSubscribed(channel string) bool
- func (c *Client) ReadPump()
- func (c *Client) SendMessage(msg *Message) error
- func (c *Client) Subscribe(channel string) bool
- func (c *Client) Unsubscribe(channel string) bool
- func (c *Client) WritePump()
- type ErrorData
- type Handler
- type Hub
- func (h *Hub) Broadcast(channel string, msg *Message, tenantID string)
- func (h *Hub) BroadcastEvent(channel string, data any, tenantID string)
- func (h *Hub) BroadcastToTenant(tenantID string, msg *Message)
- func (h *Hub) DeliverLocal(msg *BroadcastMessage)
- func (h *Hub) GetChannelsByPrefix(prefix string) []string
- func (h *Hub) GetClientsByTenant(tenantID string) []*Client
- func (h *Hub) GetStats() HubStats
- func (h *Hub) RegisterClient(client *Client)
- func (h *Hub) Run(ctx context.Context)
- func (h *Hub) SetAuthorizeFunc(fn AuthorizeFunc)
- func (h *Hub) SetPublisher(p BroadcastPublisher)
- func (h *Hub) UnregisterClient(client *Client)
- type HubStats
- type Message
- type MessageType
- type RedisBridge
- type SubscribeRequest
- type UnsubscribeRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MakeChannel ¶
func MakeChannel(channelType ChannelType, id string) string
MakeChannel creates a channel string from type and ID.
Types ¶
type AuthorizeFunc ¶
AuthorizeFunc is a function that checks if a client can subscribe to a channel. Returns true if authorized, false otherwise.
type BridgeConfig ¶ added in v0.2.0
type BridgeConfig struct {
// Channel overrides the default pubsub channel. Leave empty to use
// the standard value.
Channel string
// Logger for bridge diagnostics.
Logger *logger.Logger
}
BridgeConfig configures the Redis bridge.
type BroadcastMessage ¶
type BroadcastMessage struct {
Channel string
Message *Message
TenantID string // If set, only clients in this tenant receive the message
}
BroadcastMessage represents a message to broadcast to a channel.
type BroadcastPublisher ¶ added in v0.2.0
type BroadcastPublisher interface {
Publish(ctx context.Context, msg *BroadcastMessage) error
}
BroadcastPublisher is the minimum surface a cross-pod transport must provide. The redis-pubsub implementation lives in bridge.go.
type ChannelType ¶
type ChannelType string
ChannelType represents the type of channel.
const ( // Channel types ChannelTypeFinding ChannelType = "finding" // finding:{id} - activity updates for a finding ChannelTypeScan ChannelType = "scan" // scan:{id} - scan progress updates ChannelTypeTenant ChannelType = "tenant" // tenant:{id} - tenant-wide notifications ChannelTypeNotification ChannelType = "notification" // notification:{tenant_id} - notification delivery ChannelTypeTriage ChannelType = "triage" // triage:{finding_id} - AI triage progress updates ChannelTypeGroup ChannelType = "group" // group:{id} - group membership/scope rule changes )
func ParseChannel ¶
func ParseChannel(channel string) (ChannelType, string)
ParseChannel extracts the channel type and ID from a channel string. Channel format: "{type}:{id}" e.g., "finding:abc-123"
type Client ¶
type Client struct {
// Identity
ID string
UserID string
TenantID string
// contains filtered or unexported fields
}
Client represents a single WebSocket connection.
func (*Client) GetSubscriptions ¶
GetSubscriptions returns all subscribed channels.
func (*Client) IsSubscribed ¶
IsSubscribed checks if client is subscribed to a channel.
func (*Client) ReadPump ¶
func (c *Client) ReadPump()
ReadPump pumps messages from the WebSocket connection to the hub.
func (*Client) SendMessage ¶
SendMessage sends a message to the client.
func (*Client) Subscribe ¶
Subscribe adds a channel subscription. Returns false if already subscribed or rate limit exceeded.
func (*Client) Unsubscribe ¶
Unsubscribe removes a channel subscription.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles WebSocket connections.
func NewHandler ¶
NewHandler creates a new WebSocket handler.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub maintains the set of active clients and broadcasts messages to them.
func (*Hub) Broadcast ¶
Broadcast sends a message to all clients subscribed to a channel.
F-7: when a cross-pod publisher is configured (Redis pubsub bridge), the message is handed to it instead of being placed directly on the local broadcast channel. The bridge round-trips it through Redis and every subscribing pod (including this one) receives it via DeliverLocal, ensuring a single source of fan-out and correct delivery in multi-replica deployments.
func (*Hub) BroadcastEvent ¶
BroadcastEvent is a convenience method to broadcast an event to a channel.
func (*Hub) BroadcastToTenant ¶
BroadcastToTenant sends a message to all clients in a tenant.
func (*Hub) DeliverLocal ¶ added in v0.2.0
func (h *Hub) DeliverLocal(msg *BroadcastMessage)
DeliverLocal pushes a BroadcastMessage onto the local broadcast channel WITHOUT re-publishing it through the cross-pod publisher. This is the entry point used by the Redis subscriber to inject incoming messages into this pod's in-memory fan-out. External callers should use Broadcast instead.
func (*Hub) GetChannelsByPrefix ¶
GetChannelsByPrefix returns all channels matching a prefix.
func (*Hub) GetClientsByTenant ¶
GetClientsByTenant returns all clients for a tenant.
func (*Hub) RegisterClient ¶
RegisterClient registers a new client.
func (*Hub) SetAuthorizeFunc ¶
func (h *Hub) SetAuthorizeFunc(fn AuthorizeFunc)
SetAuthorizeFunc sets a custom authorization function.
func (*Hub) SetPublisher ¶ added in v0.2.0
func (h *Hub) SetPublisher(p BroadcastPublisher)
SetPublisher attaches a cross-pod publisher (F-7). Must be called before Run so Broadcast observes it.
func (*Hub) UnregisterClient ¶
UnregisterClient unregisters a client.
type HubStats ¶
type HubStats struct {
TotalClients int `json:"total_clients"`
TotalChannels int `json:"total_channels"`
ChannelClients map[string]int `json:"channel_clients"`
}
HubStats contains hub statistics.
type Message ¶
type Message struct {
Type MessageType `json:"type"`
Channel string `json:"channel,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
Timestamp int64 `json:"timestamp"`
RequestID string `json:"request_id,omitempty"`
}
Message is the base WebSocket message structure.
func NewMessage ¶
func NewMessage(msgType MessageType) *Message
NewMessage creates a new message with current timestamp.
func (*Message) WithChannel ¶
WithChannel sets the channel for the message.
func (*Message) WithRequestID ¶
WithRequestID sets the request ID for the message.
type MessageType ¶
type MessageType string
MessageType defines the type of WebSocket message.
const ( // Client -> Server messages MessageTypeSubscribe MessageType = "subscribe" MessageTypeUnsubscribe MessageType = "unsubscribe" MessageTypePing MessageType = "ping" // Server -> Client messages MessageTypePong MessageType = "pong" MessageTypeSubscribed MessageType = "subscribed" MessageTypeUnsubscribed MessageType = "unsubscribed" MessageTypeEvent MessageType = "event" MessageTypeError MessageType = "error" )
type RedisBridge ¶ added in v0.2.0
type RedisBridge struct {
// contains filtered or unexported fields
}
RedisBridge publishes local broadcasts to Redis and fans incoming Redis messages back into the local Hub.
func NewRedisBridge ¶ added in v0.2.0
func NewRedisBridge(rc *redislib.Client, hub *Hub, cfg *BridgeConfig) *RedisBridge
NewRedisBridge constructs a bridge. The caller is expected to call Start in a goroutine and Stop on shutdown.
func (*RedisBridge) Publish ¶ added in v0.2.0
func (b *RedisBridge) Publish(ctx context.Context, msg *BroadcastMessage) error
Publish implements BroadcastPublisher. Serialises the BroadcastMessage and pushes to the configured Redis pubsub channel.
func (*RedisBridge) Start ¶ added in v0.2.0
func (b *RedisBridge) Start(ctx context.Context) error
Start subscribes to the Redis channel and fans incoming messages into the local Hub's broadcast queue. Returns when ctx is cancelled.
Reconnection is handled by the underlying go-redis client for transient failures. For non-transient errors (pubsub closed) we log and return so the caller can restart us.
type SubscribeRequest ¶
type SubscribeRequest struct {
Channel string `json:"channel"`
RequestID string `json:"request_id,omitempty"`
}
SubscribeRequest represents a subscribe message from client.
type UnsubscribeRequest ¶
type UnsubscribeRequest struct {
Channel string `json:"channel"`
RequestID string `json:"request_id,omitempty"`
}
UnsubscribeRequest represents an unsubscribe message from client.