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: Jul 29, 2026 License: MIT Imports: 5 Imported by: 0

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