chat

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: AGPL-3.0 Imports: 22 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 chat is text chat between a viewer and the person at the host, during a session. docs/design/session-chat.md is the design.

Wire protocol

Service "chat", in feature "chat". The viewer opens it with {"v": 1}. The host answers {"hello": {"max": MaxRunes}} or {"error": CODE} (and closes). Then, in any order, on the same stream:

  • viewer to host: {"id": N, "text": "..."}; the host answers {"ack": N} or {"ack": N, "error": CODE}.
  • host to viewer: {"msg": {"text": "...", "at": UNIX_MS}}.
  • host to viewer: {"error": "chat_off"} before the host ends the stream because chat was switched off.

Every message is a wire JSON message of at most MaxMessage bytes. Each end checks the other's text with Clean; message text is never logged or written to disk.

Index

Constants

View Source
const (
	FromViewer = "viewer"
	FromHost   = "host"
)

From says who wrote a message.

View Source
const (
	// MaxRunes is the most characters (code points) one message may have.
	MaxRunes = 2000
	// MaxMessage bounds one JSON message on a chat stream.
	MaxMessage = 16 << 10
)
View Source
const (
	CodeEmpty      = "empty"
	CodeTooLong    = "too_long"
	CodeBadUTF8    = "bad_utf8"
	CodeTooFast    = "too_fast"
	CodeOff        = "chat_off"
	CodeBadRequest = "bad_request"
)

Codes a host answers with.

View Source
const Service = "chat"

Service is the stream service name.

Variables

View Source
var (
	ErrNoThread     = errors.New("chat: no such session")
	ErrNotListening = errors.New("chat: that viewer has no chat open")
)

Errors of Hub.Send.

View Source
var (
	ErrEmpty       = errors.New("chat: the message is empty")
	ErrTooLong     = errors.New("chat: the message is too long")
	ErrBadUTF8     = errors.New("chat: the message is not valid UTF-8 text")
	ErrTooFast     = errors.New("chat: too many messages, wait a moment")
	ErrOff         = errors.New("chat: the host's owner switched chat off")
	ErrUnsupported = errors.New("chat: the host does not offer chat")
	ErrClosed      = errors.New("chat: the conversation has ended")
)

Errors for the codes, and for a host without chat.

View Source
var Default = NewHub()

Default is the hub of this process's host, served as the "chat" service.

Functions

func Clean

func Clean(s string) (string, error)

Clean checks and tidies message text: it must be valid UTF-8 (and not contain U+FFFD, which a JSON decoder puts where invalid bytes were); line breaks become "\n"; other control characters and format characters (bidirectional overrides and the like, but not the zero-width joiner and non-joiner) are removed; white space around it is trimmed. What is left must not be empty or longer than MaxRunes characters.

func Code

func Code(err error) string

Code returns the wire code for err ("" for errors that are not a host's answer).

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the viewer's end of a chat stream.

func Open

func Open(ctx context.Context, c *peer.Conn) (*Client, error)

Open starts chat with the host on c. It fails with ErrOff when the host's owner switched chat off (or did not allow it for this session), and ErrUnsupported when the host has no chat.

func (*Client) Close

func (c *Client) Close() error

Close ends the stream.

func (*Client) Done

func (c *Client) Done() <-chan struct{}

Done is closed when the stream has ended; Err then says why.

func (*Client) Err

func (c *Client) Err() error

Err is why the stream ended: ErrOff, ErrClosed, or a transport error.

func (*Client) Max

func (c *Client) Max() int

Max is the host's length cap, in characters.

func (*Client) Messages

func (c *Client) Messages() <-chan Incoming

Messages delivers the host's messages; it is closed when the stream ends.

func (*Client) Send

func (c *Client) Send(ctx context.Context, text string) error

Send sends text and waits for the host to take it. The text is checked here first, as the host will (Clean).

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub holds a host's conversations, in memory only.

func NewHub

func NewHub() *Hub

NewHub returns an empty hub.

func (*Hub) Handler

func (h *Hub) Handler() peer.Handler

Handler serves the chat service with h.

func (*Hub) MarkRead

func (h *Hub) MarkRead(session int) error

MarkRead marks everything session's viewer wrote so far as read.

func (*Hub) Messages

func (h *Hub) Messages(session int, after uint64) ([]Message, error)

Messages returns the messages of session's thread numbered after after.

func (*Hub) OnMessage

func (h *Hub) OnMessage(fn func(t Thread, m Message)) (stop func())

OnMessage calls fn for every message a viewer sends, after the hub took it, with the thread as it is then (without its messages). fn must be quick. stop ends it.

func (*Hub) Send

func (h *Hub) Send(session int, text string) (Message, error)

Send writes text from the person at the host to session's viewer.

func (*Hub) Subscribe

func (h *Hub) Subscribe() (<-chan struct{}, func())

Subscribe returns a channel that receives whenever a thread changed, and a function that ends the subscription.

func (*Hub) Summaries

func (h *Hub) Summaries() []Thread

Summaries returns the threads, oldest first, without their messages.

func (*Hub) Threads

func (h *Hub) Threads() []Thread

Threads returns a copy of the threads, oldest first, with their messages.

type Incoming

type Incoming struct {
	Text string
	At   time.Time
}

Incoming is a message from the person at the host.

type Message

type Message struct {
	// N numbers the hub's messages from 1, across threads.
	N    uint64 `json:"n"`
	From string `json:"from"`
	Text string `json:"text"`
	// At is when the hub took it, in Unix milliseconds.
	At int64 `json:"at"`
}

Message is one message of a thread.

type RefusedError

type RefusedError struct{ Code string }

RefusedError is a refusal with a code this version does not know.

func (*RefusedError) Error

func (e *RefusedError) Error() string

type Thread

type Thread struct {
	// Session is the host's ID of the viewer's session.
	Session int    `json:"session"`
	Name    string `json:"name"`
	// Open: the viewer has a chat stream open, so it can be written to.
	Open bool `json:"open"`
	// Ended: the session is over.
	Ended bool `json:"ended"`
	// Unread counts the viewer's messages the person at the host has not
	// marked read (MarkRead).
	Unread int `json:"unread"`
	// Last and LastAt are the number and time of the newest message kept
	// (0 when there is none).
	Last   uint64 `json:"last"`
	LastAt int64  `json:"last_at"`
	// Messages is left out of Summaries (see Hub.Messages).
	Messages []Message `json:"messages,omitempty"`
}

Thread is the conversation with one viewer session.

Jump to

Keyboard shortcuts

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