protocol

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 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.

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 {
	ClientHash string          `json:"clientHash"`
	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.

func (Authenticated) MOTDText

func (a Authenticated) MOTDText() string

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

type ChannelID

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

ChannelID answers GetChannelID.

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{}

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

type Kicked

type Kicked struct {
	Message string `json:"message"`
}

Kicked precedes a server-initiated disconnect of this user.

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 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 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"`
	MessageBuffer []json.RawMessage `json:"messageBuffer"`
	UserList      json.RawMessage   `json:"userList"`
	Notifications json.RawMessage   `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.

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 {
	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.

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"`
	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.

Jump to

Keyboard shortcuts

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