live

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package live is the event hub: services publish events and the server-sent event streams subscribe to them (SPEC D18, §8).

An Event only says that something changed, on which topic and about which room, post or user. It never carries content: a consumer re-reads what it needs from the services, which check the permissions again. The package imports nothing of Clowder, so every layer may use it.

Index

Constants

View Source
const (
	// KindPostCreated says a top-level post was written.
	KindPostCreated = "post.created"
	// KindPostUpdated says a top-level post changed: it was edited, or its
	// thread got, lost or changed a comment.
	KindPostUpdated = "post.updated"
	// KindPostDeleted says a top-level post was deleted, or went from the
	// room because it was an empty placeholder.
	KindPostDeleted = "post.deleted"
	// KindCommentCreated says a comment was written in a thread.
	KindCommentCreated = "comment.created"
	// KindCommentUpdated says a comment was edited.
	KindCommentUpdated = "comment.updated"
	// KindCommentDeleted says a comment was deleted.
	KindCommentDeleted = "comment.deleted"
	// KindReactionChanged says a reaction was added to or removed from a post
	// or comment; PostID is that post or comment.
	KindReactionChanged = "reaction.changed"
	// KindBoardChanged says a task or a column of the board of a room changed:
	// a task was written, edited, moved or deleted, or a column was added,
	// renamed, moved or deleted. It is published on BoardTopic; PostID is the
	// post or comment of the task when a task is concerned.
	KindBoardChanged = "board.changed"
	// KindRoomUpdated says a room was renamed or its description changed.
	KindRoomUpdated = "room.updated"
	// KindRoomDeleted says a room was deleted.
	KindRoomDeleted = "room.deleted"
	// KindMemberJoined says a user became a member of a room, by joining or
	// by being added.
	KindMemberJoined = "member.joined"
	// KindMemberLeft says a user stopped being a member of a room, by leaving
	// or by being removed.
	KindMemberLeft = "member.left"
	// KindNotificationCreated says a notification was made for a user. It is
	// published on UserTopic of that user; UserID is the user, RoomID the room
	// of the notification and PostID its post or comment when it has one.
	KindNotificationCreated = "notification.created"
	// KindNotificationRead says a user marked notifications as read. It is
	// published on UserTopic of that user; UserID is the user.
	KindNotificationRead = "notification.read"
	// KindMessageCreated says a message was sent in a conversation. It is
	// published on ConversationTopic; MessageID is the message.
	KindMessageCreated = "message.created"
	// KindMessageUpdated says a message was edited. It is published on
	// ConversationTopic; MessageID is the message.
	KindMessageUpdated = "message.updated"
	// KindMessageDeleted says a message was deleted and now shows as "message
	// deleted". It is published on ConversationTopic; MessageID is the message.
	KindMessageDeleted = "message.deleted"
	// KindMessageReaction says a reaction was added to or removed from a
	// message. It is published on ConversationTopic; MessageID is the message.
	KindMessageReaction = "message.reaction"
	// KindConversationUpdated says a conversation was renamed or its members
	// changed. It is published on ConversationTopic.
	KindConversationUpdated = "conversation.updated"
	// KindConversationChanged says something changed in a conversation of a
	// user that the list of conversations and the unread count show. It is
	// published on UserTopic of every member (a member who left or was added
	// included); UserID is that user and ConversationID the conversation.
	KindConversationChanged = "conversation.changed"
	// KindUserChanged says the account of a user changed in a way that open
	// streams of the user must notice: the user was disabled, got another
	// role, or a session of the user ended (a logout). It is published on
	// UserTopic of that user; UserID is the user. The streams read the session
	// again and end when it is gone or its role differs.
	KindUserChanged = "user.changed"
	// KindConversationRead says a member read a conversation up to a message.
	// It is published on ConversationTopic, which drives "Seen" (UserID is the
	// member, MessageID the message), and on UserTopic of the member, which
	// drives the unread count.
	KindConversationRead = "conversation.read"
)

The kinds of events. A kind says what happened; the topic of the event says where.

Variables

This section is empty.

Functions

func BoardTopic

func BoardTopic(roomID uint64) string

BoardTopic is the topic of the Kanban board of a room.

func ConversationTopic

func ConversationTopic(conversationID uint64) string

ConversationTopic is the topic of a private conversation.

func PostTopic

func PostTopic(rootID uint64) string

PostTopic is the topic of the thread of the top-level post rootID.

func RoomTopic

func RoomTopic(roomID uint64) string

RoomTopic is the topic of the feed and the membership of a room.

func UserTopic

func UserTopic(userID uint64) string

UserTopic is the topic of one user: their sidebar, notifications and the changes of their memberships.

Types

type Event

type Event struct {
	// Topic is where the event was published, e.g. RoomTopic(3).
	Topic string
	// Kind says what happened; it is one of the Kind constants.
	Kind string
	// RoomID is the ID of the room concerned; 0 when none.
	RoomID uint64
	// PostID is the ID of the post or comment concerned; 0 when none.
	PostID uint64
	// RootID is the ID of the top-level post of the thread of a comment
	// event; 0 when none.
	RootID uint64
	// UserID is the ID of the user concerned (the member who joined or left);
	// 0 when none.
	UserID uint64
	// ConversationID is the ID of the private conversation concerned; 0 when
	// none.
	ConversationID uint64
	// MessageID is the ID of the private message concerned; 0 when none.
	MessageID uint64
}

Event says that something changed. It is a small value and holds no content: whoever receives it re-reads what it needs from the services.

type Hub

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

Hub delivers events to the subscribers of their topic. Make it with New. It is safe for concurrent use. It starts no goroutine: Publish delivers under a lock with non-blocking sends.

func New

func New() *Hub

New makes an empty hub.

func (*Hub) Close

func (h *Hub) Close()

Close closes every subscription and makes later Subscribe calls return closed subscriptions and later Publish calls do nothing. It is safe to call more than once.

func (*Hub) Publish

func (h *Hub) Publish(ev Event)

Publish delivers the event to every subscriber of ev.Topic. It never blocks: a subscriber whose buffer is full is closed (Dropped reports true) and the event is not delivered to it. After Close it does nothing.

func (*Hub) Subscribe

func (h *Hub) Subscribe(ctx context.Context, topics ...string) *Subscription

Subscribe listens to the topics. Cancelling ctx closes the subscription and removes it from the hub. On a closed hub, or with a ctx that is done, the subscription comes back already closed.

type Recorder

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

Recorder is a test double of the hub: it keeps every event published to it, in order. It is safe for concurrent use.

func (*Recorder) Events

func (r *Recorder) Events() []Event

Events returns a copy of the recorded events, oldest first.

func (*Recorder) Publish

func (r *Recorder) Publish(ev Event)

Publish records the event.

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset forgets the recorded events.

type Subscription

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

Subscription is one listener of the hub. Read events from C until it is closed. The channel is closed when the context ends, when Close is called, when the hub is closed or when the subscriber is too slow (see Dropped).

func (*Subscription) C

func (s *Subscription) C() <-chan Event

C is the channel of events. It is closed when the subscription ends.

func (*Subscription) Close

func (s *Subscription) Close()

Close ends the subscription: the channel is closed and the hub forgets it. It is safe to call more than once and from several goroutines.

func (*Subscription) Dropped

func (s *Subscription) Dropped() bool

Dropped reports whether the hub closed the subscription because its buffer was full. The subscriber should reconnect and reload what it shows.

Jump to

Keyboard shortcuts

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