protocol

package
v1.0.0-rc.6 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 1 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 protocol hand-ports the NG Chat wire types from the server's Zod schemas (ngchat repo, src/shared/protocol). The server validates client messages with strict schemas — unknown fields are rejected — so the structs in this file must stay exactly in sync with the server. Server messages are decoded tolerantly instead (see server.go), so server-side additions never break this client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte) any

Decode maps a server frame to its typed struct. It never fails: unrecognized names and undecodable payloads come back as Unknown.

func FrameName

func FrameName(data []byte) string

FrameName returns the name discriminator of any frame, client or server, or "" when the bytes are not a named frame. Loggers use it to label frames without decoding them.

Types

type Authenticate

type Authenticate struct {
	Name  string `json:"name"`
	Token string `json:"token"`
}

Authenticate must be the first message sent on a new socket. Only Authenticate and Ping are accepted pre-auth; the server closes unauthenticated sockets after 15s.

func NewAuthenticate

func NewAuthenticate(token string) Authenticate

NewAuthenticate builds an authenticate message for the given chat JWT.

type Authenticated

type Authenticated struct {
	IsAdmin    bool            `json:"isAdmin"`
	IsChatMod  bool            `json:"isChatMod"`
	IsSiteMod  bool            `json:"isSiteMod"`
	MOTD       json.RawMessage `json:"motd"`
	ServerTime int64           `json:"serverTime"`
	UserID     int             `json:"userID"`
	Username   string          `json:"username"`
}

Authenticated confirms a successful authenticate and describes the user. Upstream's clientHash (a bundle-staleness marker for the web SPA, optional since 18.7.0 and slated for removal) is deliberately absent: a CLI has no bundle to compare it against.

func (Authenticated) MOTDText

func (a Authenticated) MOTDText() string

MOTDText returns the message of the day when it is a plain string, otherwise "".

type Away

type Away struct {
	ChannelID      int    `json:"channelID"`
	UserID         int    `json:"userID"`
	Username       string `json:"username"`
	IsAdmin        bool   `json:"isAdmin"`
	IsChatMod      bool   `json:"isChatMod"`
	IsAway         bool   `json:"isAway"`
	AwayMessage    string `json:"awayMessage"`
	AwayMessageRaw string `json:"awayMessageRaw"`
	ServerTime     int64  `json:"serverTime"`
}

Away reports a user going away (IsAway true, with a message) or coming back (IsAway false). It patches the roster entry; it also appears in the backfill buffer, where the DB copy omits isAway entirely, so backfilled ones are ambiguous and are not replayed.

type ChannelID

type ChannelID struct {
	ChannelID   int    `json:"channelID"`
	ChannelName string `json:"channelName"`
}

ChannelID answers GetChannelID.

type ChannelUser

type ChannelUser struct {
	UserID         int    `json:"userID"`
	Username       string `json:"username"`
	IsAdmin        bool   `json:"isAdmin"`
	IsChatMod      bool   `json:"isChatMod"`
	IsAway         bool   `json:"isAway"`
	AwayMessage    string `json:"awayMessage"`
	AwayMessageRaw string `json:"awayMessageRaw"`
	UserIcon       string `json:"userIcon"`
	UserPageURL    string `json:"userPageURL"`
}

ChannelUser is one roster entry. AwayMessage is server-rendered HTML, like message bodies; AwayMessageRaw is the text the user typed.

type ChatMessage

type ChatMessage struct {
	Name      string `json:"name"`
	ChannelID int    `json:"channelID"`
	Message   string `json:"message"`
}

ChatMessage sends a chat line (1..5000 chars) to a subscribed channel. Slash commands (/me, /dm, ...) are plain message text parsed server-side.

func NewChatMessage

func NewChatMessage(channelID int, text string) ChatMessage

NewChatMessage builds a message send for a channel.

type Error

type Error struct {
	Message         string `json:"message"`
	RedirectChannel string `json:"redirectChannel"`
}

Error is a generic request failure; RedirectChannel is set when a channel lookup failed and the server suggests another channel.

type GetChannelID

type GetChannelID struct {
	Name        string `json:"name"`
	ChannelName string `json:"channelName"`
}

GetChannelID resolves a channel name to its numeric ID. Unknown names come back as an Error with a RedirectChannel.

func NewGetChannelID

func NewGetChannelID(channelName string) GetChannelID

NewGetChannelID builds a channel-name lookup. Names are lowercase on the server; callers should lowercase before sending.

type IdleTimeout

type IdleTimeout struct {
	Reason     string `json:"reason"`
	ServerTime int64  `json:"serverTime"`
}

IdleTimeout precedes a disconnect for 24h without chatting; the close reason "idle timeout" must not trigger auto-reconnect.

type Kicked

type Kicked struct {
	ChannelID      int    `json:"channelID"`
	KickedByUserID int    `json:"kickedByUserID"`
	Reason         string `json:"reason"`
	ServerTime     int64  `json:"serverTime"`
}

Kicked precedes a server-initiated disconnect of this user. Reason is optional on the wire; KickedByUserID is 0 for a system kick.

type Message

type Message struct {
	Name        string   `json:"name"`
	ChannelID   int      `json:"channelID"`
	ID          *int64   `json:"id"`
	Message     string   `json:"message"`
	MessageRaw  string   `json:"messageRaw"`
	Mentions    []string `json:"mentions"`
	IsSpoiler   bool     `json:"isSpoiler"`
	IsAdmin     bool     `json:"isAdmin"`
	IsChatMod   bool     `json:"isChatMod"`
	UserIcon    string   `json:"userIcon"`
	UserID      int      `json:"userID"`
	Username    string   `json:"username"`
	UserPageURL string   `json:"userPageURL"`
	ServerTime  int64    `json:"serverTime"`
}

Message is a chat line. It also covers the meMessage, slapMessage, serverMessage and directMessage variants, which share its shape; check Name to tell them apart. Message is server-rendered HTML; MessageRaw is the sender's original text.

type Notification

type Notification struct {
	Message     string `json:"message"`
	MessageRaw  string `json:"messageRaw"`
	MessageType string `json:"messageType"`
	ServerTime  int64  `json:"serverTime"`
	UserID      int    `json:"userID"`
	Username    string `json:"username"`
}

Notification is one inbox row from Subscribed: a mention or DM that reached the user while away. MessageType is the server's notification category, not the frame name: "message", "meMessage" (which also covers a mention inside an away message), "directMessage", "modDirectMessage" or "serverMessage". Message is rendered HTML.

type Ping

type Ping struct {
	Name string   `json:"name"`
	Data PingData `json:"data"`
}

Ping is the application-level heartbeat. 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).

func NewPing

func NewPing(unixMilli int64) Ping

NewPing builds a heartbeat ping stamped with epoch milliseconds.

type PingData

type PingData struct {
	Time int64 `json:"time"`
}

PingData carries the client timestamp echoed back in Pong.

type Pong

type Pong struct {
	Data PingData `json:"data"`
}

Pong answers Ping, echoing the client timestamp.

type Reauthenticate

type Reauthenticate struct {
	Name  string `json:"name"`
	Token string `json:"token"`
}

Reauthenticate answers a server Revalidate with a freshly minted chat JWT. It is deliberately not Authenticate: the server replaces the socket's claim snapshot in place instead of re-running the join/subscribe/motd pipeline, and Authenticate's already-authenticated guard stays. 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.

func NewReauthenticate

func NewReauthenticate(token string) Reauthenticate

NewReauthenticate builds the in-place renewal message.

type Revalidate

type Revalidate struct {
	ServerTime int64 `json:"serverTime"`
}

Revalidate is the server's nudge shortly before the chat JWT's hard close deadline. Answering with Reauthenticate carrying a fresh token swaps the socket's claims in place; ignoring it leaves the unchanged close-and-reconnect path, since the deadline never moves.

type Revalidated

type Revalidated struct {
	IsAdmin    bool  `json:"isAdmin"`
	IsChatMod  bool  `json:"isChatMod"`
	IsSiteMod  bool  `json:"isSiteMod"`
	ServerTime int64 `json:"serverTime"`
}

Revalidated acknowledges a successful Reauthenticate. The privilege flags come from the new token and replace those from Authenticated, so a mod demoted on the site loses the flags without reconnecting.

type Subscribe

type Subscribe struct {
	Name      string `json:"name"`
	ChannelID int    `json:"channelID"`
}

Subscribe joins a channel by ID; the reply is Subscribed with the backfill buffer and user list.

func NewSubscribe

func NewSubscribe(channelID int) Subscribe

NewSubscribe builds a channel subscription.

type Subscribed

type Subscribed struct {
	ChannelID     int               `json:"channelID"`
	ChannelName   string            `json:"channelName"`
	MessageBuffer []json.RawMessage `json:"messageBuffer"`
	UserList      []ChannelUser     `json:"userList"`
	Notifications []Notification    `json:"notifications"`
	ServerTime    int64             `json:"serverTime"`
}

Subscribed answers Subscribe. MessageBuffer holds up to 25 recent events (same envelopes as live ones — decode each with Decode); it is the only history a client ever receives. UserList is the full roster at subscribe time, the baseline that UserJoined, UserLeft, UserUpdated and Away then patch. Notifications are the server's persistent inbox of mentions and DMs received while away, newest first, up to 100; the server never clears it, so every subscribe carries the same rows again.

type Typing

type Typing struct {
	Name      string `json:"name"`
	ChannelID int    `json:"channelID"`
}

Typing signals the user is composing; exempt from flood limits.

func NewTyping

func NewTyping(channelID int) Typing

NewTyping builds a typing indicator for a channel.

type TypingEvent

type TypingEvent struct {
	ChannelID int    `json:"channelID"`
	UserID    int    `json:"userID"`
	Username  string `json:"username"`
}

TypingEvent reports another user composing in a channel.

type Unauthorized

type Unauthorized struct {
	ChannelID int    `json:"channelID"`
	Message   string `json:"message"`
}

Unauthorized reports an auth failure. "jwt expired"/"jwt malformed" are soft failures: re-mint and re-send Authenticate on the same socket. ChannelID is set only when a subscribe was refused (channel ban, alt of a banned account); the socket stays open in that case, so the client must decide for itself that there is nothing left to do.

type Unknown

type Unknown struct {
	Name string
}

Unknown is any frame this client does not (yet) understand, including known names whose payload failed to decode. Callers must treat it as ignorable — that tolerance is what lets old binaries survive protocol additions (ADR 0002).

type Unsubscribe

type Unsubscribe struct {
	Name      string `json:"name"`
	ChannelID int    `json:"channelID"`
}

Unsubscribe leaves a channel.

func NewUnsubscribe

func NewUnsubscribe(channelID int) Unsubscribe

NewUnsubscribe builds a channel unsubscription.

type Unsubscribed

type Unsubscribed struct {
	ChannelID int `json:"channelID"`
}

Unsubscribed answers Unsubscribe.

type UserJoined

type UserJoined struct {
	ChannelID      int    `json:"channelID"`
	UserID         int    `json:"userID"`
	Username       string `json:"username"`
	IsAdmin        bool   `json:"isAdmin"`
	IsChatMod      bool   `json:"isChatMod"`
	IsAway         bool   `json:"isAway"`
	AwayMessage    string `json:"awayMessage"`
	AwayMessageRaw string `json:"awayMessageRaw"`
	UserIcon       string `json:"userIcon"`
	UserPageURL    string `json:"userPageURL"`
	ServerTime     int64  `json:"serverTime"`
}

UserJoined reports a user entering a channel.

type UserLeft

type UserLeft struct {
	ChannelID  int    `json:"channelID"`
	UserID     int    `json:"userID"`
	Username   string `json:"username"`
	ServerTime int64  `json:"serverTime"`
}

UserLeft reports a user leaving a channel.

type UserUpdated

type UserUpdated UserJoined

UserUpdated is a silent roster patch: the same shape as UserJoined but for a user already in the channel (a mid-session role or away change, typically published by an in-place token renewal). Consumers must not announce it as a join; the UI's user list applies it as a patch.

Jump to

Keyboard shortcuts

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