Code Monkey home page Code Monkey logo

phx's Introduction

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.

phx's People

Contributors

gooblitz avatar nshafer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.