protocol

package
v1.0.0-feature-modules... Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 5 Imported by: 1 module, 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(msg *ServerMessage) ([]byte, error)

Encode serializes a ServerMessage to JSON bytes.

func MatchAny

func MatchAny(patterns []string, topic string) bool

MatchAny returns true if the topic matches any of the patterns.

func MatchTopic

func MatchTopic(pattern, topic string) bool

MatchTopic checks if a topic matches a pattern. Patterns support:

  • exact match: "user.created" matches "user.created"
  • wildcard segment: "user.*" matches "user.created", "user.deleted"
  • multi-segment wildcard: "**" matches everything

Segments are separated by ".".

Types

type Message

type Message struct {
	ID        string          `json:"id"`
	Type      MessageType     `json:"type"`
	Topic     string          `json:"topic,omitempty"`
	Payload   json.RawMessage `json:"payload,omitempty"`
	Timestamp time.Time       `json:"timestamp"`
}

Message represents a client-to-server WebSocket message.

func Parse

func Parse(data []byte, maxTopicLength int) (*Message, error)

Parse decodes raw JSON bytes into a client Message and validates it.

type MessageType

type MessageType string

MessageType represents the type of a WebSocket message.

const (
	TypeSubscribe   MessageType = "subscribe"
	TypePublish     MessageType = "publish"
	TypeUnsubscribe MessageType = "unsubscribe"
	TypePing        MessageType = "ping"
	TypeAuth        MessageType = "auth"
)

Client-to-server message types.

const (
	TypeSubscribed   MessageType = "subscribed"
	TypeUnsubscribed MessageType = "unsubscribed"
	TypeMessage      MessageType = "message"
	TypeError        MessageType = "error"
	TypePong         MessageType = "pong"
	TypeAuthSuccess  MessageType = "auth_success"
)

Server-to-client message types.

type ServerMessage

type ServerMessage struct {
	ID        string          `json:"id"`
	Type      MessageType     `json:"type"`
	Topic     string          `json:"topic,omitempty"`
	Payload   json.RawMessage `json:"payload,omitempty"`
	Timestamp time.Time       `json:"timestamp"`
}

ServerMessage represents a server-to-client WebSocket message.

func NewAuthSuccessMsg

func NewAuthSuccessMsg(requestID string) *ServerMessage

NewAuthSuccessMsg creates an auth success response message.

func NewErrorMsg

func NewErrorMsg(requestID, errText string) *ServerMessage

NewErrorMsg creates an error response message.

func NewEventMsg

func NewEventMsg(topic string, payload json.RawMessage) *ServerMessage

NewEventMsg creates a server-to-client event message.

func NewPongMsg

func NewPongMsg(requestID string) *ServerMessage

NewPongMsg creates a pong response message.

func NewSubscribedMsg

func NewSubscribedMsg(requestID, topic string) *ServerMessage

NewSubscribedMsg creates a subscription confirmation message.

func NewUnsubscribedMsg

func NewUnsubscribedMsg(requestID, topic string) *ServerMessage

NewUnsubscribedMsg creates an unsubscription confirmation message.

Jump to

Keyboard shortcuts

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