Help us shape the future of Go! Take the 2026 Go Developer Survey

realtime

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MessageTypeSubscribe      = "subscribe"
	MessageTypeUnsubscribe    = "unsubscribe"
	MessageTypeChannelMessage = "message"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

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

Channel describes a room, websocket users can subscribe and sent messages to.

func (*Channel) FindContext

func (c *Channel) FindContext(clientId string, path string) (*Context, bool)

FindContext returns the context of a subscriber tied to clientId and path

func (*Channel) HandleMessage

func (c *Channel) HandleMessage(client *Client, message *Message)

HandleMessage executes the channels OnMessage method if it exists.

func (*Channel) IsSubscribed

func (c *Channel) IsSubscribed(clientId string, path string) bool

IsSubscribed returns true if the client is connected to the channel

func (*Channel) PathMatches

func (c *Channel) PathMatches(path string) (bool, map[string]string)

PathMatches returns true and the params of the channel subscription if the path matches the path of the Channel.

func (*Channel) Subscribe

func (c *Channel) Subscribe(context *Context)

Subscribe executes the Channels middlewares and(if successful) adds the user to the Channel and executes the channels OnSubscribe handler.

func (*Channel) Unsubscribe

func (c *Channel) Unsubscribe(clientId string, path string) bool

Unsubscribe removes the client from the channel and executes the OnUnsubscribe handler

func (*Channel) UnsubscribeAllPaths

func (c *Channel) UnsubscribeAllPaths(clientId string) bool

UnsubscribeAllPaths unsubscribes a client from all paths of the channel they are connected to.

type ChannelHandlers

type ChannelHandlers struct {
	OnSubscribe             EventHandlerFunc
	OnUnsubscribe           EventHandlerFunc
	OnMessage               MessageHandlerFunc
	SubscriptionMiddlewares []SubscriptionMiddleware
}

ChannelHandlers contains all handler functions for various events in the Channel.

type ChannelStore

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

ChannelStore stores pointers to all Channels

func (*ChannelStore) Get

func (s *ChannelStore) Get(path string) (bool, *Channel, map[string]string)

Get retrieves a channel for a path

func (*ChannelStore) OnMessage

func (s *ChannelStore) OnMessage(client *Client, message *Message)

OnMessage is called when a message is received on the websocket

func (*ChannelStore) Register

func (s *ChannelStore) Register(path string, handlers ChannelHandlers) *Channel

Register registers a handler on path

func (*ChannelStore) Subscribe

func (s *ChannelStore) Subscribe(client *Client, channelPath string) bool

Subscribe subscribes the client to the channelPath

func (*ChannelStore) Unsubscribe

func (s *ChannelStore) Unsubscribe(clientId string, channelPath string) bool

Unsubscribe removes a client from a channel

func (*ChannelStore) UnsubscribeAll

func (s *ChannelStore) UnsubscribeAll(clientId string)

UnsubscribeAll removes a client from all channels

type ChannelSubscribers

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

ChannelSubscribers holds a reference to all subscribers of a channel.

func (*ChannelSubscribers) Add

func (subs *ChannelSubscribers) Add(context *Context)

Add adds a ClientContext to the channels subscribers

func (*ChannelSubscribers) GetContext

func (subs *ChannelSubscribers) GetContext(clientId string, path string) (*Context, bool)

GetContext returns the context for a client of the channels subscribers

func (*ChannelSubscribers) IsSubscribed

func (subs *ChannelSubscribers) IsSubscribed(clientId string, path string) bool

IsSubscribed checks if a client is subscribed to a channel

func (*ChannelSubscribers) Remove

func (subs *ChannelSubscribers) Remove(clientId string, path string)

Remove removes a client from the channels subscribers

func (*ChannelSubscribers) RemoveAllPaths

func (subs *ChannelSubscribers) RemoveAllPaths(clientId string) []*Context

RemoveAllPaths removes a client from all channels subscribers

type Client

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

Client is a subscriber one or multiple channels.

func NewClient

func NewClient(sendMessage MessageSendFunc, properties map[string]interface{}) *Client

NewClient creates a Client

func (*Client) Get

func (client *Client) Get(key string) (value interface{}, exists bool)

Get returns a property from the client's properties or (any, false)

func (*Client) Send

func (client *Client) Send(message []byte) error

Send sends the message using the client's MessageSendFunc.

func (*Client) Set

func (client *Client) Set(key string, value interface{})

Set sets a property on the client's properties.

type ClientStore

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

func (*ClientStore) Exists

func (c *ClientStore) Exists(id string) bool

func (*ClientStore) Get

func (c *ClientStore) Get(id string) *Client

func (*ClientStore) Join

func (c *ClientStore) Join(client *Client)

func (*ClientStore) NextId

func (c *ClientStore) NextId() string

func (*ClientStore) Remove

func (c *ClientStore) Remove(id string)

type ConnectHookFunc

type ConnectHookFunc func(*Client)

ConnectHookFunc is called on connecting.

type Connector

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

Connector manages a set of clients

func NewConnector

func NewConnector(requestHandler RequestHandlerFunc) *Connector

NewConnector creates a connector for the given RequestHandlerFunc

func (*Connector) Join

func (c *Connector) Join(sendMessage MessageSendFunc, properties map[string]interface{}) *Client

Join To be triggered if a client connects via ws

func (*Connector) Leave

func (c *Connector) Leave(clientId string)

Leave triggers the clients OnDisconnect handler and removes it from the Connector

func (*Connector) Message

func (c *Connector) Message(clientId string, data []byte)

Message triggers the clients OnMessage handler

type Context

type Context struct {
	Client   *Client
	FullPath string
	// contains filtered or unexported fields
}

Context carries params and properties across a clients lifetime.

func (*Context) Get

func (context *Context) Get(key string) (value interface{}, exists bool)

Get returns a key from the Context

func (*Context) Param

func (context *Context) Param(key string) string

Param returns a param by key.

func (*Context) Send

func (context *Context) Send(payload []byte) error

Send sends a generic payload to the Context's client.

func (*Context) SendError

func (context *Context) SendError(error *Error) error

SendError flushes an error to the Context's client.

func (*Context) Set

func (context *Context) Set(key string, value interface{})

Set sets a key on the Context

func (*Context) SetParams

func (context *Context) SetParams(params map[string]string)

SetParams sets params on Context

type DisconnectHookFunc

type DisconnectHookFunc func(*Client)

DisconnectHookFunc is called on disconnecting.

type Error

type Error struct {
	Code        int    `json:"code"`
	Description string `json:"description"`
}

Error represents an error response.

func NewError

func NewError(code int, description string) *Error

NewError creates the error based on code and description.

type EventHandlerFunc

type EventHandlerFunc func(s *Context)

EventHandlerFunc is a function that is executed when subscribing or unsubscribing to the Channel.

type Hooks

type Hooks struct {
	OnConnect    ConnectHookFunc
	OnDisconnect DisconnectHookFunc
	OnMessage    MessageHookFunc
}

Hooks manages the Connector's Hooks

type Message

type Message struct {
	Type    string          `json:"type"`
	Channel string          `json:"channel"`
	Payload json.RawMessage `json:"payload"`
}

type MessageHandlerFunc

type MessageHandlerFunc func(s *Context, message *Message)

MessageHandlerFunc is a function that executes when a message is sent to the Channel.

type MessageHookFunc

type MessageHookFunc func(*Client, []byte)

MessageHookFunc is called on Message.

type MessageSendFunc

type MessageSendFunc func(message []byte) error

MessageSendFunc is a function that sends a message over the network.

type Realtime

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

func New

func New(connector *Connector) *Realtime

New Creates a new Realtime instance

func (*Realtime) CloseAll

func (r *Realtime) CloseAll()

func (*Realtime) HandleRequest

func (r *Realtime) HandleRequest(writer http.ResponseWriter, request *http.Request, properties map[string]interface{}) error

HandleRequest handles a new websocket request, adds the properties to the new client

func (*Realtime) IsConnected

func (r *Realtime) IsConnected(clientId string) bool

func (*Realtime) IsSubscribed

func (r *Realtime) IsSubscribed(channelPath string, clientId string) bool

IsSubscribed checks whether a client is subscribed to a certain channelPath or not

func (*Realtime) RegisterChannel

func (r *Realtime) RegisterChannel(channelName string, handlers ChannelHandlers) *Channel

RegisterChannel registers a new channel

func (*Realtime) Send

func (r *Realtime) Send(channelPath string, clientId string, payload []byte) error

type RequestHandlerFunc

type RequestHandlerFunc func(writer http.ResponseWriter, request *http.Request, properties map[string]interface{}) error

RequestHandlerFunc is a func that handles a request.

type SubscriptionMiddleware

type SubscriptionMiddleware func(s *Context) *Error

SubscriptionMiddleware is a function that is executed when a client connects to a Channel. If the middleware returns a non nil Error, the subscription won't be finished.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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