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

realtime

package
v1.4.21 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 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)

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)

func (*ChannelStore) OnMessage

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

func (*ChannelStore) Register

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

func (*ChannelStore) Subscribe

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

func (*ChannelStore) Unsubscribe

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

func (*ChannelStore) UnsubscribeAll

func (s *ChannelStore) UnsubscribeAll(clientId string)

type ChannelSubscribers

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

func (*ChannelSubscribers) Add

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

func (*ChannelSubscribers) GetContext

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

func (*ChannelSubscribers) IsSubscribed

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

func (*ChannelSubscribers) Remove

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

func (*ChannelSubscribers) RemoveAllPaths

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

type Client

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

func NewClient

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

func (*Client) Get

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

func (*Client) Send

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

func (*Client) Set

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

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)

type Connector

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

func NewConnector

func NewConnector(requestHandler RequestHandlerFunc) *Connector

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)

func (*Connector) Message

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

type Context

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

func (*Context) Get

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

func (*Context) Param

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

func (*Context) Send

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

func (*Context) SendError

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

func (*Context) Set

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

func (*Context) SetParams

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

type DisconnectHookFunc

type DisconnectHookFunc func(*Client)

type Error

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

func NewError

func NewError(code int, description string) *Error

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
}

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)

type MessageSendFunc

type MessageSendFunc func(message []byte) error

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) 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

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