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 ¶
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.
const ( // DropOverflow is the reason of a subscriber whose buffer was full. DropOverflow = "overflow" // DropReconnect is the reason of a subscription closed by DropAll: the // bridge to Redis came back after a break and events may have been // missed. DropReconnect = "reconnect" )
The reasons a subscription is dropped (see Subscription.DropReason).
Variables ¶
This section is empty.
Functions ¶
func BoardTopic ¶
BoardTopic is the topic of the Kanban board of a room.
func ConversationTopic ¶
ConversationTopic is the topic of a private conversation.
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 (*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) DropAll ¶ added in v1.1.0
func (h *Hub) DropAll()
DropAll closes every current subscription as if it were too slow: Dropped reports true on each and DropReason says DropReconnect, so their subscribers reconnect and reload what they show. The hub itself stays open: later Subscribe and Publish calls work as before. The bridge between processes calls it when its connection to Redis came back after a break, because events may have been missed (SPEC D41). On a closed hub it does nothing.
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.
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) AddTopics ¶ added in v1.1.0
func (s *Subscription) AddTopics(topics ...string)
AddTopics makes the subscription listen to more topics, with no gap: an event published after the call returns is delivered, and nothing is delivered twice. A topic it already listens to is left as it is. On a closed subscription it does nothing.
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) DropReason ¶ added in v1.1.0
func (s *Subscription) DropReason() string
DropReason says why the hub closed the subscription: DropOverflow when its buffer was full, DropReconnect after DropAll. It is empty for a subscription that is open, or that ended by Close, by its context or by the hub's Close.
func (*Subscription) Dropped ¶
func (s *Subscription) Dropped() bool
Dropped reports whether the hub closed the subscription because its buffer was full, or because of DropAll. The subscriber should reconnect and reload what it shows.
func (*Subscription) RemoveTopics ¶ added in v1.1.0
func (s *Subscription) RemoveTopics(topics ...string)
RemoveTopics makes the subscription stop listening to the topics. Events of them that are already in the channel are still delivered. A topic it does not listen to is ignored. On a closed subscription it does nothing.