Documentation
¶
Overview ¶
Package protocol holds the NG Chat wire types. The structs in protocol_gen.go are generated from the JSON Schema spec ngchat derives from its Zod schemas (src/shared/protocol/protocol.schema.json, ADR 0010); regenerate with NGCHAT_UPSTREAM=<checkout> go generate.
The server strict-validates client frames, rejecting unknown fields, so what this package sends must match the spec exactly. Server frames are decoded tolerantly instead (Decode, ADR 0002), so server-side additions never break this client.
Index ¶
- Constants
- func Decode(data []byte) any
- func FrameName(data []byte) string
- type ClientAuthenticateFrame
- type ClientDirectTypingFrame
- type ClientGetChannelIDFrame
- type ClientMessageFrame
- type ClientPingFrame
- type ClientPingFrameData
- type ClientReauthenticateFrame
- type ClientSubscribeFrame
- type ClientTypingFrame
- type ClientUnsubscribeFrame
- type Message
- type ServerAirhornFrame
- type ServerAuthenticatedFrame
- type ServerAwayFrame
- type ServerChannelIDFrame
- type ServerDirectMessageFrame
- type ServerDirectTypingFrame
- type ServerErrorFrame
- type ServerIdleTimeoutFrame
- type ServerKickedFrame
- type ServerMeMessageFrame
- type ServerMessageEmbedsFrame
- type ServerMessageEmbedsFrameEmbedsItem
- type ServerMessageFrame
- type ServerMessageFrameEmbedsItem
- type ServerPlaySoundEffectFrame
- type ServerPlaySoundboardTriggerFrame
- type ServerPongFrame
- type ServerPongFrameData
- type ServerRevalidateFrame
- type ServerRevalidatedFrame
- type ServerServerMessageFrame
- type ServerSlapMessageFrame
- type ServerSubscribedFrame
- type ServerSubscribedFrameNotificationsItem
- type ServerSubscribedFrameUserListItem
- type ServerTypingFrame
- type ServerUnauthorizedFrame
- type ServerUnsubscribedFrame
- type ServerUserJoinedFrame
- type ServerUserLeftFrame
- type ServerUserUpdatedFrame
- type Unknown
Constants ¶
const ( MaxAwayMessageRawLength = 256 MaxAwayMessageRenderedBytes = 32768 MaxFrameBytes = 67108864 MaxRosterRowsInBudget = 10000 SubscribedBackfillRows = 25 SubscribedNotificationRows = 25 )
Size limits from the spec's x-limits: the server's contract for how large frames and their lists may grow.
const ( ClientAuthenticateFrameName = "authenticate" ClientMessageFrameName = "message" ClientDirectTypingFrameName = "directTyping" ClientGetChannelIDFrameName = "getChannelID" ClientPingFrameName = "ping" ClientReauthenticateFrameName = "reauthenticate" ClientSubscribeFrameName = "subscribe" ClientTypingFrameName = "typing" ClientUnsubscribeFrameName = "unsubscribe" ServerAirhornFrameName = "airhorn" ServerAuthenticatedFrameName = "authenticated" ServerAwayFrameName = "away" ServerChannelIDFrameName = "channelID" ServerErrorFrameName = "error" ServerIdleTimeoutFrameName = "idleTimeout" ServerKickedFrameName = "kicked" ServerMessageFrameName = "message" ServerDirectMessageFrameName = "directMessage" ServerDirectTypingFrameName = "directTyping" ServerMeMessageFrameName = "meMessage" ServerMessageEmbedsFrameName = "messageEmbeds" ServerPlaySoundEffectFrameName = "playSoundEffect" ServerPlaySoundboardTriggerFrameName = "playSoundboardTrigger" ServerPongFrameName = "pong" ServerRevalidateFrameName = "revalidate" ServerRevalidatedFrameName = "revalidated" ServerServerMessageFrameName = "serverMessage" ServerSlapMessageFrameName = "slapMessage" ServerSubscribedFrameName = "subscribed" ServerTypingFrameName = "typing" ServerUserJoinedFrameName = "userJoined" ServerUserLeftFrameName = "userLeft" ServerUserUpdatedFrameName = "userUpdated" ServerUnsubscribedFrameName = "unsubscribed" )
Frame names: the name discriminator each frame carries on the wire.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ClientAuthenticateFrame ¶
ClientAuthenticateFrame is the client→server "authenticate" frame.
First frame on a new socket, carrying a chat JWT. Accepted once per socket; swap the token on an authenticated socket with `reauthenticate` instead.
func NewAuthenticate ¶
func NewAuthenticate(token string) ClientAuthenticateFrame
NewAuthenticate builds the first frame on a new socket. Only authenticate and ping are accepted pre-auth; the server closes unauthenticated sockets after 15s.
type ClientDirectTypingFrame ¶
type ClientDirectTypingFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
ToUsername string `json:"toUsername"`
}
ClientDirectTypingFrame is the client→server "directTyping" frame.
Tells one user, rather than the whole channel, that the sender is writing them a direct message. Kept apart from `typing` so a client that only knows the channel frame never mistakes it for channel typing. The server drops it silently when the recipient is offline, blocked, or ignoring the sender, so it never reveals anything a DM attempt would not.
type ClientGetChannelIDFrame ¶
type ClientGetChannelIDFrame struct {
Name string `json:"name"`
ChannelName string `json:"channelName"`
}
ClientGetChannelIDFrame is the client→server "getChannelID" frame.
func NewGetChannelID ¶
func NewGetChannelID(channelName string) ClientGetChannelIDFrame
NewGetChannelID builds a channel-name lookup. Names are lowercase on the server; callers should lowercase before sending. Unknown names come back as an error frame with a redirectChannel.
type ClientMessageFrame ¶
type ClientMessageFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
IsMarkdown *bool `json:"isMarkdown,omitempty"`
Message string `json:"message"`
}
ClientMessageFrame is the client→server "message" frame.
func NewChatMessage ¶
func NewChatMessage(channelID int64, text string) ClientMessageFrame
NewChatMessage builds a chat line (1..5000 chars) for a subscribed channel. Slash commands (/me, /dm, ...) are plain message text parsed server-side.
type ClientPingFrame ¶
type ClientPingFrame struct {
Name string `json:"name"`
Data ClientPingFrameData `json:"data"`
}
ClientPingFrame is the client→server "ping" frame.
Keepalive. The server answers with `pong` echoing `data`.
func NewPing ¶
func NewPing(unixMilli int64) ClientPingFrame
NewPing builds a heartbeat stamped with epoch milliseconds. The server terminates any socket with no inbound traffic for 15 seconds, so clients must ping continuously (the browser client uses a 1-5s cadence).
type ClientPingFrameData ¶
type ClientPingFrameData struct {
Time int64 `json:"time"`
}
ClientPingFrameData is the ClientPingFrame.data object.
type ClientReauthenticateFrame ¶
ClientReauthenticateFrame is the client→server "reauthenticate" frame.
Answers a server `revalidate` with a freshly minted token. Deliberately distinct from `authenticate`: the server replaces the socket's claim snapshot in place rather than re-running the join/subscribe/motd pipeline, and the already-authenticated guard on `authenticate` stays.
func NewReauthenticate ¶
func NewReauthenticate(token string) ClientReauthenticateFrame
NewReauthenticate answers a server revalidate with a freshly minted chat JWT. The server refuses a token for a different account or one whose expiry is not strictly later than the token in force, and caps attempts per token (~5), so send at most one per revalidate.
type ClientSubscribeFrame ¶
ClientSubscribeFrame is the client→server "subscribe" frame.
func NewSubscribe ¶
func NewSubscribe(channelID int64) ClientSubscribeFrame
NewSubscribe joins a channel by ID; the reply is a subscribed frame with the backfill buffer and the roster.
type ClientTypingFrame ¶
ClientTypingFrame is the client→server "typing" frame.
The sender is typing in `channelID`. Rebroadcast to the channel as a server `typing` frame.
func NewTyping ¶
func NewTyping(channelID int64) ClientTypingFrame
NewTyping signals the user is composing; exempt from flood limits.
type ClientUnsubscribeFrame ¶
ClientUnsubscribeFrame is the client→server "unsubscribe" frame.
func NewUnsubscribe ¶
func NewUnsubscribe(channelID int64) ClientUnsubscribeFrame
NewUnsubscribe leaves a channel.
type Message ¶
type Message struct {
Name string
ChannelID int64
ID *int64
Message string
MessageRaw string
Mentions []string
IsSpoiler bool
IsAdmin bool
IsChatMod bool
UserIcon string
UserID int64
Username string
UserPageURL string
ServerTime int64
}
Message is a chat line: one view of the message, meMessage, slapMessage, serverMessage and directMessage frames, which gap detection and the transcript treat as one thing. Check Name to tell them apart. Message is server-rendered HTML; MessageRaw is the sender's original text. Decode builds it from the generated frame structs, so a field a frame does not carry is left zero: only message has an ID or IsSpoiler, and meMessage and serverMessage carry no user decoration.
type ServerAirhornFrame ¶
type ServerAirhornFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
ServerTime int64 `json:"serverTime"`
}
ServerAirhornFrame is the server→client "airhorn" frame.
type ServerAuthenticatedFrame ¶
type ServerAuthenticatedFrame struct {
Name string `json:"name"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsSiteMod bool `json:"isSiteMod"`
MOTD string `json:"motd"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
Username string `json:"username"`
}
ServerAuthenticatedFrame is the server→client "authenticated" frame.
type ServerAwayFrame ¶
type ServerAwayFrame struct {
Name string `json:"name"`
AwayMessage string `json:"awayMessage,omitempty"`
AwayMessageRaw string `json:"awayMessageRaw,omitempty"`
ChannelID int64 `json:"channelID"`
IsAdmin bool `json:"isAdmin"`
IsAway *bool `json:"isAway,omitempty"`
IsChatMod bool `json:"isChatMod"`
Mentions []string `json:"mentions,omitempty"`
Message string `json:"message,omitempty"`
MessageRaw string `json:"messageRaw,omitempty"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerAwayFrame is the server→client "away" frame.
type ServerChannelIDFrame ¶
type ServerChannelIDFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
ChannelName string `json:"channelName"`
ServerTime int64 `json:"serverTime"`
}
ServerChannelIDFrame is the server→client "channelID" frame.
type ServerDirectMessageFrame ¶
type ServerDirectMessageFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
ServerTime int64 `json:"serverTime"`
ToUsername string `json:"toUsername"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerDirectMessageFrame is the server→client "directMessage" frame.
type ServerDirectTypingFrame ¶
type ServerDirectTypingFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
ServerTime int64 `json:"serverTime"`
ToUsername string `json:"toUsername"`
UserID int64 `json:"userID"`
Username string `json:"username"`
}
ServerDirectTypingFrame is the server→client "directTyping" frame.
Sent only to the recipient's instances in the channel: `username` is writing them a direct message. `toUsername` echoes the name the sender addressed, so a client holding several sessions can tell it is the target.
type ServerErrorFrame ¶
type ServerErrorFrame struct {
Name string `json:"name"`
Message string `json:"message,omitempty"`
RedirectChannel string `json:"redirectChannel,omitempty"`
ServerTime int64 `json:"serverTime"`
}
ServerErrorFrame is the server→client "error" frame.
type ServerIdleTimeoutFrame ¶
type ServerIdleTimeoutFrame struct {
Name string `json:"name"`
Reason string `json:"reason"`
ServerTime int64 `json:"serverTime"`
}
ServerIdleTimeoutFrame is the server→client "idleTimeout" frame.
The user sent no chat messages for too long. The server closes the socket about 100 ms later. Admins and chat mods are exempt.
type ServerKickedFrame ¶
type ServerKickedFrame struct {
Name string `json:"name"`
ChannelID *int64 `json:"channelID,omitempty"`
KickedByUserID *int64 `json:"kickedByUserID,omitempty"`
Reason string `json:"reason,omitempty"`
ServerTime int64 `json:"serverTime"`
}
ServerKickedFrame is the server→client "kicked" frame.
Sent to each of the user's sockets in `channelID`, or on the whole server when `channelID` is absent, just before the server closes them.
type ServerMeMessageFrame ¶
type ServerMeMessageFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
Mentions []string `json:"mentions"`
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
ServerTime int64 `json:"serverTime"`
// Set only on the `/sfx` "X played ..." line: the soundboard trigger this
// row narrates. A client that dropped the matching `playSoundboardTrigger`
// hides the row too, so the transcript never claims a sound nobody heard.
TriggerID string `json:"triggerId,omitempty"`
UserID int64 `json:"userID"`
Username string `json:"username"`
}
ServerMeMessageFrame is the server→client "meMessage" frame.
type ServerMessageEmbedsFrame ¶
type ServerMessageEmbedsFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
Embeds []ServerMessageEmbedsFrameEmbedsItem `json:"embeds"`
ID int64 `json:"id"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
}
ServerMessageEmbedsFrame is the server→client "messageEmbeds" frame.
Link embeds for the earlier chat `message` with this `id`, resolved after it was sent. Not sent when no embed resolves.
type ServerMessageEmbedsFrameEmbedsItem ¶
ServerMessageEmbedsFrameEmbedsItem is one element of ServerMessageEmbedsFrame.embeds.
type ServerMessageFrame ¶
type ServerMessageFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
Embeds []ServerMessageFrameEmbedsItem `json:"embeds,omitempty"`
ID *int64 `json:"id,omitempty"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsSpoiler bool `json:"isSpoiler"`
Mentions []string `json:"mentions"`
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
ServerTime int64 `json:"serverTime"`
SoundEffectsTriggered []string `json:"soundEffectsTriggered,omitempty"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerMessageFrame is the server→client "message" frame.
type ServerMessageFrameEmbedsItem ¶
ServerMessageFrameEmbedsItem is one element of ServerMessageFrame.embeds.
type ServerPlaySoundEffectFrame ¶
type ServerPlaySoundEffectFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
EffectName string `json:"effectName"`
Loop *bool `json:"loop,omitempty"`
ServerTime int64 `json:"serverTime"`
Sprite string `json:"sprite,omitempty"`
Volume *float64 `json:"volume,omitempty"`
}
ServerPlaySoundEffectFrame is the server→client "playSoundEffect" frame.
type ServerPlaySoundboardTriggerFrame ¶
type ServerPlaySoundboardTriggerFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
ServerTime int64 `json:"serverTime"`
TriggerID string `json:"triggerId"`
// Who asked for the sound. Optional so frames from an older server still
// validate. Lets a client show a "sound skipped" notice only to the user
// who triggered a sound it dropped.
UserID *int64 `json:"userID,omitempty"`
}
ServerPlaySoundboardTriggerFrame is the server→client "playSoundboardTrigger" frame.
type ServerPongFrame ¶
type ServerPongFrame struct {
Name string `json:"name"`
Data ServerPongFrameData `json:"data"`
ServerTime int64 `json:"serverTime"`
}
ServerPongFrame is the server→client "pong" frame.
type ServerPongFrameData ¶
type ServerPongFrameData struct {
Time int64 `json:"time"`
}
ServerPongFrameData is the ServerPongFrame.data object.
type ServerRevalidateFrame ¶
type ServerRevalidateFrame struct {
Name string `json:"name"`
ServerTime int64 `json:"serverTime"`
}
ServerRevalidateFrame is the server→client "revalidate" frame.
Nudge sent shortly before the authenticated token's close deadline. The client answers with `reauthenticate` carrying a freshly minted token so the socket's claim snapshot is replaced in place instead of the socket being closed and reconnected. Clients that ignore it fall back to the close/reconnect path.
type ServerRevalidatedFrame ¶
type ServerRevalidatedFrame struct {
Name string `json:"name"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsSiteMod bool `json:"isSiteMod"`
ServerTime int64 `json:"serverTime"`
}
ServerRevalidatedFrame is the server→client "revalidated" frame.
Acknowledges a successful `reauthenticate`. Carries the privilege flags from the new token so a role change (a demoted mod, say) reaches the UI without a reload.
type ServerServerMessageFrame ¶
type ServerServerMessageFrame struct {
Name string `json:"name"`
ChannelID *int64 `json:"channelID,omitempty"`
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
ServerTime int64 `json:"serverTime"`
}
ServerServerMessageFrame is the server→client "serverMessage" frame.
type ServerSlapMessageFrame ¶
type ServerSlapMessageFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
Mentions []string `json:"mentions"`
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerSlapMessageFrame is the server→client "slapMessage" frame.
type ServerSubscribedFrame ¶
type ServerSubscribedFrame struct {
Name string `json:"name"`
AwayMessage string `json:"awayMessage"`
AwayMessageRaw string `json:"awayMessageRaw"`
// Deprecated compat shim with a fixed value. Ignore it.
CelebrationType string `json:"celebrationType,omitempty"`
ChannelID int64 `json:"channelID"`
ChannelName string `json:"channelName"`
// Deprecated compat shim with a fixed value. Ignore it.
ComicChat *bool `json:"comicChat,omitempty"`
IsAway bool `json:"isAway"`
// Each element is one raw frame (away, message, meMessage, serverMessage,
// slapMessage); decode it with Decode.
MessageBuffer []json.RawMessage `json:"messageBuffer"`
Notifications []ServerSubscribedFrameNotificationsItem `json:"notifications"`
ServerTime int64 `json:"serverTime"`
UserList []ServerSubscribedFrameUserListItem `json:"userList"`
}
ServerSubscribedFrame is the server→client "subscribed" frame.
The channel state, sent when the socket joins a channel: recent `messageBuffer` backfill, notifications, and the `userList` roster. The largest frame the server sends; see `x-limits`.
type ServerSubscribedFrameNotificationsItem ¶
type ServerSubscribedFrameNotificationsItem struct {
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
MessageType string `json:"messageType"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
Username string `json:"username"`
}
ServerSubscribedFrameNotificationsItem is one element of ServerSubscribedFrame.notifications.
type ServerSubscribedFrameUserListItem ¶
type ServerSubscribedFrameUserListItem struct {
AwayMessage string `json:"awayMessage"`
AwayMessageRaw string `json:"awayMessageRaw"`
IsAdmin bool `json:"isAdmin"`
IsAway bool `json:"isAway"`
IsChatMod bool `json:"isChatMod"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerSubscribedFrameUserListItem is one element of ServerSubscribedFrame.userList.
A roster row. `awayMessage` is a plain, HTML-escaped preview of the away text, not the rendered markup the `away` frame carries, so a roster stays bounded whatever the formatter does.
type ServerTypingFrame ¶
type ServerTypingFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
Username string `json:"username"`
}
ServerTypingFrame is the server→client "typing" frame.
Rebroadcast of a client `typing` frame to the channel.
type ServerUnauthorizedFrame ¶
type ServerUnauthorizedFrame struct {
}
ServerUnauthorizedFrame is the server→client "unauthorized" frame.
type ServerUnsubscribedFrame ¶
type ServerUnsubscribedFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
Username string `json:"username"`
}
ServerUnsubscribedFrame is the server→client "unsubscribed" frame.
type ServerUserJoinedFrame ¶
type ServerUserJoinedFrame struct {
Name string `json:"name"`
AwayMessage string `json:"awayMessage"`
AwayMessageRaw string `json:"awayMessageRaw"`
ChannelID int64 `json:"channelID"`
IsAdmin bool `json:"isAdmin"`
IsAway bool `json:"isAway"`
IsChatMod bool `json:"isChatMod"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerUserJoinedFrame is the server→client "userJoined" frame.
type ServerUserLeftFrame ¶
type ServerUserLeftFrame struct {
Name string `json:"name"`
ChannelID int64 `json:"channelID"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerUserLeftFrame is the server→client "userLeft" frame.
type ServerUserUpdatedFrame ¶
type ServerUserUpdatedFrame struct {
Name string `json:"name"`
AwayMessage string `json:"awayMessage"`
AwayMessageRaw string `json:"awayMessageRaw"`
ChannelID int64 `json:"channelID"`
IsAdmin bool `json:"isAdmin"`
IsAway bool `json:"isAway"`
IsChatMod bool `json:"isChatMod"`
ServerTime int64 `json:"serverTime"`
UserID int64 `json:"userID"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
Username string `json:"username"`
}
ServerUserUpdatedFrame is the server→client "userUpdated" frame.
A roster entry that changed while the user stayed in the channel, today a role change picked up by an in-place token renewal. Same shape as `userJoined` so clients can reuse their upsert, but a distinct name: a refresh must not play the join sound or print a join notice, and clients that predate it drop it rather than announcing a join that never happened.
Directories
¶
| Path | Synopsis |
|---|---|
|
Command gen writes internal/protocol/protocol_gen.go from the JSON Schema spec in the upstream ngchat checkout, invoked by the //go:generate directive in internal/protocol/doc.go (ADR 0010).
|
Command gen writes internal/protocol/protocol_gen.go from the JSON Schema spec in the upstream ngchat checkout, invoked by the //go:generate directive in internal/protocol/doc.go (ADR 0010). |