Go client for Phoenix Channels
MIT License
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.
This module requires Go 1.18 or later.
go get github.com/nshafer/phx
API documentation can be viewed via godoc at API Documentation.
Examples are in examples/.
Please see Releases on Github.
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)
})
There is also a simple CLI example for interactively using the library to connect to any server/channel in examples/cli/cli.go.