Categorygithub.com/gooblitz/phx
modulepackage
0.2.2
Repository: https://github.com/gooblitz/phx.git
Documentation: pkg.go.dev

# README

Phx - A Phoenix Channels client for Go

Go Reference

This is a comprehensive client for Phoenix Channels written for Go applications. The goal of this project is to be a reliable, resilient, full-featured client library for connecting to Phoenix servers over websockets and being able to push and receive events from one or more channels in a performant and concurrent manner.

This module is based off of the official JavaScript client implementation, except where deviations were needed due to being written in Go. But if you're familiar with connecting to Phoenix Channels in JS, then this library should feel right at home.

Installation

This module requires Go 1.18 or later.

go get github.com/nshafer/phx

Documentation

API documentation can be viewed via godoc at API Documentation.

Examples are in examples/.

Features

  • Supports websockets as the transport method. Longpoll is not currently supported, nor are there plans to implement it.
  • Supports the JSONSerializerV2 serializer. (JSONSerializerV1 also available if preferred.)
  • All event handlers are simple functions that are registered with the Socket, Channels or Pushes. No complicated interfaces to implement.
  • Completely concurrent using many goroutines in the background so that your main thread is not blocked. All callbacks will run in separate goroutines.
  • Supports setting connection parameters, headers, proxy, etc on the main websocket connection.
  • Supports passing parameters when joining a Channel
  • Pluggable Transport, TransportHandler, Logger if needed.

Simple example

For a more complete example, see examples/simple/simple.go. This example does not include error handling.

// Connect to socket
endPoint, _ := url.Parse("ws://localhost:4000/socket?foo=bar")
socket := phx.NewSocket(endPoint)
_ = socket.Connect()

// Join a channel
channel := socket.Channel("test:lobby", nil)
join, _ := channel.Join()
join.Receive("ok", func(response any) {
  log.Println("Joined channel:", channel.Topic(), response)
})

// Send an event
ping, _ := channel.Push("ping", "pong")
ping.Receive("ok", func(response any) {
  log.Println("Ping:", response)
})

// Listen for pushes or broadcasts from the server
channel.On("shout", func(payload any) {
  log.Println("received shout:", payload)
})

CLI example

There is also a simple CLI example for interactively using the library to connect to any server/channel in examples/cli/cli.go.

Not implemented currently:

  • Longpoll transport.
  • Binary messages.

# Functions

NewChannel creates a new Channel attached to the Socket.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewPush gets a new Push ready to send and allows you to attach event handlers for replies, errors, timeouts.
NewSimpleLogger returns a CustomLogger that uses the 'log' package's DefaultLogger to log messages above the given logLevel.
NewSocket creates a Socket that connects to the given endPoint using the default websocket Transport.
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
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.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
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.
HeartBeatEvent is a special message for heartbeats on the special topic "phoenix".
JoinEvent is sent when we join a channel.
LeaveEvent is sent by the client when we leave a channel and unsubscribe to a topic.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ReplyEvent is sent by the server in reply to any event sent by the client.

# Structs

A Channel is a unique connection to the given Topic on the server.
CustomLogger is a logger that logs to the given log.Logger if the message is >= logLevel.
JSONMessage is a JSON representation of a Message.
No description provided by the author
No description provided by the author
Message is a message sent or received via the socket after encoding/decoding.
Push allows you to send an Event to the server and easily monitor for replies, errors or timeouts.
A Socket represents a connection to the server via the given Transport.
Websocket is a Transport that connects to the server via Websockets.

# Interfaces

No description provided by the author
A Serializer describes the required interface for serializers.
Transport is used by a Socket to actually connect to the server.
TransportHandler defines the interface that handles the activity of the Transport.

# Type aliases

No description provided by the author
No description provided by the author
Event represents a phoenix channel event for a message, and can be almost anything the user wants, such as "ping", "shout", "talk", etc.
No description provided by the author
NoopLogger is a logger that does nothing.
Ref is a unique reference integer that is atomically incremented and will wrap at 64 bits + 1.