Documentation ¶
Overview ¶
Package phx is a comprehensive client for Phoenix Channels written for Go applications.
Index ¶
- Constants
- type Channel
- func (c *Channel) Clear(event string)
- func (c *Channel) IsClosed() bool
- func (c *Channel) IsErrored() bool
- func (c *Channel) IsJoined() bool
- func (c *Channel) IsJoining() bool
- func (c *Channel) IsLeaving() bool
- func (c *Channel) IsRemoved() bool
- func (c *Channel) Join() (*Push, error)
- func (c *Channel) JoinRef() Ref
- func (c *Channel) Leave() (*Push, error)
- func (c *Channel) Off(bindingRef Ref)
- func (c *Channel) On(event string, callback func(payload any)) (bindingRef Ref)
- func (c *Channel) OnClose(callback func(payload any)) (bindingRef Ref)
- func (c *Channel) OnError(callback func(payload any)) (bindingRef Ref)
- func (c *Channel) OnJoin(callback func(payload any)) (bindingRef Ref)
- func (c *Channel) OnRef(ref Ref, event string, callback func(payload any)) (bindingRef Ref)
- func (c *Channel) Push(event string, payload any) (*Push, error)
- func (c *Channel) Remove() error
- func (c *Channel) State() ChannelState
- func (c *Channel) Topic() string
- type ChannelState
- type ConnectionState
- type CustomLogger
- type Event
- type JSONMessage
- type JSONSerializerV1
- type JSONSerializerV2
- type Logger
- type LoggerLevel
- type Message
- type NoopLogger
- type Push
- type Ref
- type Serializer
- type Socket
- func (s *Socket) Channel(topic string, params map[string]string) *Channel
- func (s *Socket) Connect() error
- func (s *Socket) ConnectionState() ConnectionState
- func (s *Socket) Disconnect() error
- func (s *Socket) IsConnected() bool
- func (s *Socket) IsConnectedOrConnecting() bool
- func (s *Socket) MakeRef() Ref
- func (s *Socket) Off(ref Ref)
- func (s *Socket) OnClose(callback func()) Ref
- func (s *Socket) OnError(callback func(error)) Ref
- func (s *Socket) OnMessage(callback func(Message)) Ref
- func (s *Socket) OnOpen(callback func()) Ref
- func (s *Socket) Push(topic string, event string, payload any, joinRef Ref) (Ref, error)
- func (s *Socket) PushMessage(msg Message) error
- func (s *Socket) Reconnect() error
- type Transport
- type TransportHandler
- type Websocket
- func (w *Websocket) Connect(endPoint *url.URL, requestHeader http.Header, connectTimeout time.Duration) error
- func (w *Websocket) ConnectionState() ConnectionState
- func (w *Websocket) Disconnect() error
- func (w *Websocket) IsConnected() bool
- func (w *Websocket) Reconnect() error
- func (w *Websocket) Send(msg []byte) error
Constants ¶
const ( LogDebug LoggerLevel = 0 LogInfo = 1 LogWarning = 2 LogError = 3 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel struct { // PushTimeout is the time that a Push waits before considering it defunct and triggering a "timeout" event. PushTimeout time.Duration // RejoinAfterFunc is a function that returns the duration to wait before rejoining based on given tries RejoinAfterFunc func(tries int) time.Duration // contains filtered or unexported fields }
A Channel is a unique connection to the given Topic on the server. You can have many Channels connected over one Socket, each handling messages independently.
func NewChannel ¶
NewChannel creates a new Channel attached to the Socket. If there is already a Channel for the given topic, that channel is returned instead of creating a new one.
func (*Channel) Join ¶
Join will send a JoinEvent to the server and attempt to join the topic of this Channel. A Push is returned to which you can attach event handlers to with Receive, such as "ok", "error" and "timeout".
func (*Channel) JoinRef ¶
JoinRef returns the JoinRef for this channel, which is the Ref of the Push returned by Join
func (*Channel) Leave ¶
Leave will send a LeaveEvent to the server to leave the topic of this Channel A Push is returned to which you can attach event handlers to with Receive, such as "ok", "error" and "timeout".
func (*Channel) Off ¶
Off removes the callback for the given bindingRef, as returned by On, OnRef, OnJoin, OnClose, OnError.
func (*Channel) On ¶
On will register the given callback for all matching events received on this Channel. Returns a unique Ref that can be used to cancel this callback via Off.
func (*Channel) OnClose ¶
OnClose will register the given callback for whenever this Channel is closed. Returns a unique Ref that can be used to cancel this callback via Off.
func (*Channel) OnError ¶
OnError will register the given callback for whenever this channel gets an ErrorEvent message, such as the channel process crashing. Returns a unique Ref that can be used to cancel this callback via Off.
func (*Channel) OnJoin ¶
OnJoin will register the given callback for whenever this Channel joins successfully to the server. Returns a unique Ref that can be used to cancel this callback via Off.
func (*Channel) OnRef ¶
OnRef will register the given callback for all matching events only if the ref also matches. This is mostly used by Push so that it can get ReplyEvents for its messages. Returns a unique Ref that can be used to cancel this callback via Off.
func (*Channel) Push ¶
Push will send the given Event and Payload to the server. A Push is returned to which you can attach event handlers to with Receive() so you can process replies.
func (*Channel) Remove ¶
Remove will remove this channel from the Socket. Once this is done, the channel will no longer receive any kind of messages, and be essentially orphaned, so it is important that you also remove all references to the Channel so that it can be garbage collected.
func (*Channel) State ¶
func (c *Channel) State() ChannelState
State returns the current ChannelState of this channel. Can also use Is*() functions
type ChannelState ¶
type ChannelState int
const ( ChannelClosed ChannelState = iota ChannelErrored ChannelJoined ChannelJoining ChannelLeaving ChannelRemoved )
func (ChannelState) String ¶
func (c ChannelState) String() string
type ConnectionState ¶
type ConnectionState int
const ( ConnectionConnecting ConnectionState = iota ConnectionOpen ConnectionClosing ConnectionClosed )
func (ConnectionState) String ¶
func (s ConnectionState) String() string
type CustomLogger ¶
type CustomLogger struct {
// contains filtered or unexported fields
}
CustomLogger is a logger that logs to the given log.Logger if the message is >= logLevel
func NewCustomLogger ¶
func NewCustomLogger(level LoggerLevel, logger *log.Logger) *CustomLogger
func NewSimpleLogger ¶
func NewSimpleLogger(logLevel LoggerLevel) *CustomLogger
NewSimpleLogger returns a CustomLogger that uses the 'log' package's DefaultLogger to log messages above the given logLevel
func (*CustomLogger) Print ¶
func (l *CustomLogger) Print(level LoggerLevel, kind string, v ...any)
func (*CustomLogger) Printf ¶
func (l *CustomLogger) Printf(level LoggerLevel, kind string, format string, v ...any)
func (*CustomLogger) Println ¶
func (l *CustomLogger) Println(level LoggerLevel, kind string, v ...any)
type Event ¶
type Event string
Event represents a phoenix channel event for a message, and can be almost anything the user wants, such as "ping", "shout", "talk", etc. There are several reserved for control messages for protocol overhead.
const ( // JoinEvent is sent when we join a channel. JoinEvent Event = "phx_join" // CloseEvent is sent by the server when a channel is closed, such as before shutting down the socket // This event is also generated by the client whenever `channel.Leave()` is called. Triggers channel.OnClose(). CloseEvent Event = "phx_close" // ErrorEvent is sent by the server whenever something catastrophic happens on the server side, such as the channel // process crashing, or attempting to join a channel already joined. ErrorEvent Event = "phx_error" // ReplyEvent is sent by the server in reply to any event sent by the client. ReplyEvent Event = "phx_reply" // LeaveEvent is sent by the client when we leave a channel and unsubscribe to a topic. LeaveEvent Event = "phx_leave" // HeartBeatEvent is a special message for heartbeats on the special topic "phoenix" HeartBeatEvent Event = "heartbeat" )
Special/reserved Events
type JSONMessage ¶
type JSONMessage struct { JoinRef string `json:"join_ref,omitempty"` Ref string `json:"ref"` Topic string `json:"topic"` Event string `json:"event"` Payload any `json:"payload"` }
JSONMessage is a JSON representation of a Message
func NewJSONMessage ¶
func NewJSONMessage(msg Message) *JSONMessage
func (*JSONMessage) Message ¶
func (jm *JSONMessage) Message() (*Message, error)
type JSONSerializerV1 ¶
type JSONSerializerV1 struct{}
func NewJSONSerializerV1 ¶
func NewJSONSerializerV1() *JSONSerializerV1
func (*JSONSerializerV1) Version ¶
func (s *JSONSerializerV1) Version() string
type JSONSerializerV2 ¶
type JSONSerializerV2 struct{}
func NewJSONSerializerV2 ¶
func NewJSONSerializerV2() *JSONSerializerV2
func (*JSONSerializerV2) Version ¶
func (s *JSONSerializerV2) Version() string
type Logger ¶
type Logger interface { Print(level LoggerLevel, kind string, v ...any) Println(level LoggerLevel, kind string, v ...any) Printf(level LoggerLevel, kind string, format string, v ...any) }
type LoggerLevel ¶
type LoggerLevel int
func (LoggerLevel) String ¶
func (level LoggerLevel) String() string
type Message ¶
type Message struct { // JoinRef is the unique Ref sent when a JoinEvent is sent to join a channel. JoinRef can also be though of as // a Channel ref. If present, this message is tied to the given instance of a Channel. JoinRef Ref // Ref is the unique Ref for a given message. When sending a new Message, a Ref should be generated. When a reply // is sent back from the server, it will have Ref set to match the Message it is a reply to. Ref Ref // Topic is the Channel topic this message is in relation to, as defined on the server side. Topic string // Event is a string description of what this message is about, and can be set to anything the user desires. // Some Events are reserved for specific protocol messages as defined in event.go. Event string // Payload is any arbitrary data attached to the message. Payload any }
Message is a message sent or received via the socket after encoding/decoding
func (Message) MarshalJSON ¶
func (*Message) UnmarshalJSON ¶
type NoopLogger ¶
type NoopLogger int
NoopLogger is a logger that does nothing
func NewNoopLogger ¶
func NewNoopLogger() *NoopLogger
func (*NoopLogger) Print ¶
func (l *NoopLogger) Print(_ LoggerLevel, _ string, _ ...any)
func (*NoopLogger) Printf ¶
func (l *NoopLogger) Printf(_ LoggerLevel, _ string, _ string, _ ...any)
func (*NoopLogger) Println ¶
func (l *NoopLogger) Println(_ LoggerLevel, _ string, _ ...any)
type Push ¶
type Push struct { // Event is the string event you want to push to the server. Event string // Payload is whatever payload you want to attach to the push. Must be JSON serializable. Payload any // Timeout is the time to wait for a reply before triggering a "timeout" event. Timeout time.Duration Ref Ref // contains filtered or unexported fields }
Push allows you to send an Event to the server and easily monitor for replies, errors or timeouts. A Push is typically created by Channel.Join, Channel.Leave and Channel.Push.
func NewPush ¶
NewPush gets a new Push ready to send and allows you to attach event handlers for replies, errors, timeouts.
func (*Push) Receive ¶
Receive registers the given event handler for the given status. Built in Events such as Join, Leave will respond with "ok", "error" and "timeout". Custom event handlers (handle_in/3) in your Channel on the server can respond with any string event they want. If a custom event handler (handle_in/3) does not reply (returns :noreply) then the only events that will trigger here are "error" and "timeout".
type Ref ¶
type Ref uint64
Ref is a unique reference integer that is atomically incremented and will wrap at 64 bits + 1
type Serializer ¶
type Serializer interface { Version() string Encode(*Message) ([]byte, error) Decode([]byte) (*Message, error) }
A Serializer describes the required interface for serializers
type Socket ¶
type Socket struct { // Endpoint is the URL to connect to. Include parameters here. EndPoint *url.URL // RequestHeader is an http.Header map to send in the initial connection. RequestHeader http.Header // Transport is the main transport mechanism to use to connect to the server. Only Websocket is implemented currently. Transport Transport // Specify a logger for Errors, Warnings, Info and Debug messages. Defaults to phx.NoopLogger. Logger Logger // Timeout for initial handshake with server. ConnectTimeout time.Duration // ReconnectAfterFunc is a function that returns the time to delay reconnections based on the given tries ReconnectAfterFunc func(tries int) time.Duration // HeartbeatInterval is the duration between heartbeats sent to the server to keep the connection alive. HeartbeatInterval time.Duration // Serializer encodes/decodes messages to/from the server. Must work with a Serializer on the server. // Defaults to JSONSerializerV2 Serializer Serializer // contains filtered or unexported fields }
A Socket represents a connection to the server via the given Transport. Many Channels can be connected over a single Socket.
func NewSocket ¶
NewSocket creates a Socket that connects to the given endPoint using the default websocket Transport. After creating the socket, several options can be set, such as Transport, Logger, Serializer and timeouts.
If a custom websocket.Dialer is needed, such as to set up a Proxy, then create a custom WebSocket
func (*Socket) Channel ¶
Channel creates a new instance of phx.Channel, or returns an existing instance if it had already been created.
func (*Socket) Connect ¶
Connect will start connection attempts with the server until successful or canceled with Disconnect.
func (*Socket) ConnectionState ¶
func (s *Socket) ConnectionState() ConnectionState
func (*Socket) Disconnect ¶
Disconnect or stop trying to Connect to server.
func (*Socket) IsConnected ¶
func (*Socket) IsConnectedOrConnecting ¶
func (*Socket) OnClose ¶
OnClose registers the given callback to be called whenever the Socket is closed Returns a unique Ref that can be used to cancel this callback via Off.
func (*Socket) OnError ¶
OnError registers the given callback to be called whenever the Socket has an error Returns a unique Ref that can be used to cancel this callback via Off.
func (*Socket) OnMessage ¶
OnMessage registers the given callback to be called whenever the server sends a message Returns a unique Ref that can be used to cancel this callback via Off.
func (*Socket) OnOpen ¶
OnOpen registers the given callback to be called whenever the Socket is opened successfully. Returns a unique Ref that can be used to cancel this callback via Off.
func (*Socket) PushMessage ¶
type Transport ¶
type Transport interface { Connect(endPoint *url.URL, requestHeader http.Header, connectTimeout time.Duration) error Disconnect() error Reconnect() error IsConnected() bool ConnectionState() ConnectionState Send([]byte) error }
Transport is used by a Socket to actually connect to the server.
type TransportHandler ¶
type TransportHandler interface {
// contains filtered or unexported methods
}
TransportHandler defines the interface that handles the activity of the Transport. This is usually just a Socket, but a custom TransportHandler can be implemented to stand in between a Transport and Socket.
type Websocket ¶
type Websocket struct { Dialer *websocket.Dialer Handler TransportHandler // contains filtered or unexported fields }
Websocket is a Transport that connects to the server via Websockets.
func NewWebsocket ¶
func NewWebsocket(handler TransportHandler) *Websocket
func (*Websocket) ConnectionState ¶
func (w *Websocket) ConnectionState() ConnectionState