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
- func BoardTopic(roomID uint64) string
- func ConversationTopic(conversationID uint64) string
- func PostTopic(rootID uint64) string
- func RoomTopic(roomID uint64) string
- func UserTopic(userID uint64) string
- type Event
- type Hub
- type MemoryPresence
- type Option
- type Presence
- type Recorder
- type Subscription
Constants ¶
const ( // KindPostCreated says a top-level post was written. It is also published // on UserTopic of each room member whose preferences make it ring (those // whose choice for the room is All, SPEC §7.15), with Ring and Sound set; // UserID is that member. 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, PostID its post or comment when it has one and // NotificationID the notification. Ring, Sound and Browser say whether it // rings for that user. KindNotificationCreated = "notification.created" // KindNotificationChanged says a notification of a user changed: a reply // was folded into it, so that it names a newer actor and counts more // people (SPEC §7.15). It is published on UserTopic of that user; UserID // is the user, RoomID the room, PostID the post of the notification and // NotificationID the notification. It never rings. KindNotificationChanged = "notification.changed" // 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. It is also // published on UserTopic of each member whose preferences make the message // ring (SPEC §7.15), with Ring and Sound set; UserID is that member. 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" // KindUserUpdated says the profile of a user changed: the display name or // the avatar. It is published on UserTopic of that user only (SPEC §7.14); // UserID is the user. The pages of the user show the new profile in the // sidebar; a client of the WebSocket gets the user object. KindUserUpdated = "user.updated" // KindSettingsChanged says the settings of a user changed (the theme, the // sound, the browser notifications, what notifies). It is published on // UserTopic of that user; UserID is the user. A client of the WebSocket // gets the settings. KindSettingsChanged = "settings.changed" // KindPlacePrefsChanged says the notification preferences of a user in one // room or conversation changed (SPEC §7.15). It is published on UserTopic // of that user; UserID is the user, RoomID the room or ConversationID the // conversation. The sidebar and the list of conversations refresh, and a // client of the WebSocket gets the preferences. KindPlacePrefsChanged = "place_prefs.changed" // KindRoomUnread says the unread counts of a room changed for one member: // a post came or was deleted, a mention came or was read, or the member's // mark moved (SPEC §7.17, D55). It is published on UserTopic of that member; // UserID is the member and RoomID the room. It holds no counts: whoever // receives it reads the counts of that room for that user. KindRoomUnread = "room.unread" // 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" // KindCallRinging says a user is being called (SPEC §7.18). It is // published on UserTopic of the callee; CallID is the call and // ConversationID its conversation. The pages show the ringing dialog and // the WebSocket sends the call. KindCallRinging = "call.ringing" // KindCallStarted says a call was started or answered: it is published on // UserTopic of both users when the call begins to ring and again when the // callee answers; the state of the call tells which. CallID and // ConversationID name the call. KindCallStarted = "call.started" // KindCallUpdated says a participant of an active call turned the // microphone or the camera on or off. It is published on UserTopic of both // users; UserID is the participant and Microphone and Camera are their // state. KindCallUpdated = "call.updated" // KindCallEnded says a call is over, however it ended. It is published on // UserTopic of both users; CallID and ConversationID name the call. The // media server closes the call on it. KindCallEnded = "call.ended" )
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
// NotificationID is the ID of the notification of a notification.created
// event; 0 for the others.
NotificationID uint64
// CallID is the ID of the call concerned; 0 when none.
CallID uint64
// Microphone and Camera are the state of the participant UserID in a
// call.updated event: true when the microphone is on, true when the camera
// is on. They mean nothing for any other kind.
Microphone, Camera bool
// Ring is true when the event, published on the topic of one user,
// rings for that user: the page plays the sound and shows the browser
// notification as Sound and Browser say. The server decides it per
// recipient from the preferences (SPEC §7.15); the page only obeys.
Ring bool
// Sound is the name of the sound to play when Ring is true ("purr",
// "chirp", "bell" or "meow"); empty for no sound.
Sound string
// Browser is true when Ring is true and the user wants notifications of
// the browser.
Browser bool
}
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 MemoryPresence ¶ added in v1.2.0
type MemoryPresence struct {
// contains filtered or unexported fields
}
MemoryPresence is a Presence that counts the connections of the users of one process in memory. Make it with NewMemoryPresence. It is safe for concurrent use and starts no goroutine.
func NewMemoryPresence ¶ added in v1.2.0
func NewMemoryPresence() *MemoryPresence
NewMemoryPresence makes a presence with nobody online.
func (*MemoryPresence) Connect ¶ added in v1.2.0
func (p *MemoryPresence) Connect(userID uint64)
Connect implements Presence.
func (*MemoryPresence) Disconnect ¶ added in v1.2.0
func (p *MemoryPresence) Disconnect(userID uint64)
Disconnect implements Presence.
func (*MemoryPresence) Local ¶ added in v1.2.0
func (p *MemoryPresence) Local() []uint64
Local returns the users who are online in this process, for a Presence that keeps the rest elsewhere: each user once, in no particular order.
type Option ¶ added in v1.2.0
type Option func(*Hub)
Option changes how New makes a hub.
func WithDropCounter ¶ added in v1.2.0
func WithDropCounter(dropped *metrics.CounterVec) Option
WithDropCounter makes the hub count the subscriptions it closes, by reason (DropOverflow, DropReconnect), in the counter vec, which has one label, the reason (SPEC D60). A nil vec counts nothing.
type Presence ¶ added in v1.2.0
type Presence interface {
// Connect records one more open connection of the user.
Connect(userID uint64)
// Disconnect records that one connection of the user closed. A
// Disconnect without a Connect does nothing.
Disconnect(userID uint64)
// Online returns, for each of the given users who is online now, true.
// A user who is not online has no key in the map. It fails only when the
// shared store cannot be read; the in-memory one never fails.
Online(ctx context.Context, userIDs []uint64) (map[uint64]bool, error)
}
Presence says who is online (SPEC §7.15). A user is online while at least one page stream or WebSocket of theirs is open, in any process: the owner of the hub tells it when one opens (Connect) and when it closes (Disconnect), and anyone can ask who of some users is online now.
MemoryPresence counts in memory, for one process; cluster.Presence keeps the data in Redis, so that the answer covers every process that shares it. Both pass the same tests (package presencetest).
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.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package presencetest holds the table of tests that every live.Presence implementation must pass, so that the in-memory one and the one over Redis behave the same.
|
Package presencetest holds the table of tests that every live.Presence implementation must pass, so that the in-memory one and the one over Redis behave the same. |