ws

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MintTicket

func MintTicket(secret []byte, userID, accountID string, now time.Time) (ticket string, expiresAt time.Time, err error)

MintTicket signs a short-lived WebSocket connection ticket for an already-authenticated user/account pair.

func NewHandler

func NewHandler(hub *Hub, authClient *grpcclient.AuthServiceClient, notificationClient *grpcclient.NotificationServiceClient, ticketSecret []byte) http.HandlerFunc

NewHandler returns an http.HandlerFunc that upgrades HTTP connections to WebSocket, authenticates the connection, and starts the client read/write pumps. Authentication is cookie-based (cookie + account ID query param) for first-party origins; connections from custom portal domains cannot send the auth cookie cross-origin, so they instead present a short-lived ticket minted by the cookie-authenticated ticket endpoint. notificationClient may be nil (conversation-subscribe authz is then unavailable). ticketSecret may be nil (ticket auth is then disabled).

func NewTicketHandler

func NewTicketHandler(authClient *grpcclient.AuthServiceClient, ticketSecret []byte) http.HandlerFunc

NewTicketHandler returns an http.HandlerFunc that mints short-lived WebSocket tickets for cookie-authenticated callers. Custom portal domains reach it through the frontend's same-origin API proxy (so the auth cookie is present), then open the WebSocket cross-origin to the API host with the ticket instead of the cookie. When ticketSecret is empty the endpoint is disabled.

func StartEventConsumer

func StartEventConsumer(ctx context.Context, broker messaging.MessageBroker, hub *Hub) error

StartEventConsumer starts a RabbitMQ consumer that reads agent run step events and fans them out to WebSocket clients via the hub.

func StartNotificationConsumer

func StartNotificationConsumer(ctx context.Context, broker messaging.MessageBroker, hub *Hub) error

StartNotificationConsumer starts a RabbitMQ consumer that reads in-app notification / messaging realtime-delivery events and fans them out to WebSocket clients via the hub, routing to the per-user topic (the bell) and/or a conversation topic (live chat).

func StartRunCompletedConsumer

func StartRunCompletedConsumer(ctx context.Context, broker messaging.MessageBroker, hub *Hub) error

StartRunCompletedConsumer starts a RabbitMQ consumer that reads agent run completed events and fans them out to WebSocket clients via the hub.

func VerifyTicket

func VerifyTicket(secret []byte, ticket string, now time.Time) (userID, accountID string, err error)

VerifyTicket validates a ticket's signature and expiry and returns the identity it carries.

Types

type Client

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

Client represents a single WebSocket connection.

func NewClient

func NewClient(conn *websocket.Conn, hub *Hub, accountID, userID string, checkParticipant ParticipantChecker) *Client

NewClient creates a new Client.

func (*Client) ReadPump

func (c *Client) ReadPump(ctx context.Context)

ReadPump reads messages from the WebSocket connection and handles subscribe, unsubscribe, and ping commands.

func (*Client) SubscribeAccountTopic

func (c *Client) SubscribeAccountTopic()

SubscribeAccountTopic subscribes the client to its account's broadcast topic so account-wide announcements arrive live. Called on connect.

func (*Client) SubscribeUserGlobalTopic

func (c *Client) SubscribeUserGlobalTopic()

SubscribeUserGlobalTopic subscribes the client to its account-independent user topic, used for cross-account unread hints. Called on connect.

func (*Client) SubscribeUserTopic

func (c *Client) SubscribeUserTopic()

SubscribeUserTopic subscribes the client to its own notification (bell) topic. Called on connect so a user receives notifications without an explicit subscribe message.

func (*Client) WritePump

func (c *Client) WritePump(ctx context.Context)

WritePump drains the send channel and writes messages to the WebSocket.

type Hub

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

Hub manages WebSocket client subscriptions keyed by topic string. Topics namespace the different event streams: "run:<agent_run_id>" (agent run events), "user:<user_id>" (a user's notification bell), and "conv:<conversation_id>" (live chat, future). It fans out incoming events to all clients subscribed to a topic, with tenant isolation via account ID checks.

func NewHub

func NewHub() *Hub

NewHub creates a new Hub.

func (*Hub) Publish

func (h *Hub) Publish(topic, accountID string, event []byte)

Publish sends an event to all clients subscribed to the given topic. Only clients whose account ID matches the event's account ID receive the message, ensuring tenant isolation.

func (*Hub) PublishGlobal

func (h *Hub) PublishGlobal(topic string, event []byte)

PublishGlobal sends an event to all clients on a topic WITHOUT the account-isolation check. It is only safe for inherently user-scoped topics (userglobal:<user_id>), where every subscriber is the same user, and is used to carry cross-account unread hints to a user's connections regardless of which account they are currently viewing.

func (*Hub) RemoveClient

func (h *Hub) RemoveClient(client *Client)

RemoveClient unsubscribes a client from all topics it was subscribed to.

func (*Hub) Subscribe

func (h *Hub) Subscribe(topic string, client *Client)

Subscribe registers a client for events on the given topic.

func (*Hub) Unsubscribe

func (h *Hub) Unsubscribe(topic string, client *Client)

Unsubscribe removes a client from events on the given topic.

type ParticipantChecker

type ParticipantChecker func(ctx context.Context, conversationID string) (bool, error)

ParticipantChecker reports whether this connection's user may subscribe to a conversation topic. It is the WS authz gate for conv:<id> (the server fans message events to that topic).

Jump to

Keyboard shortcuts

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