anoskov / phoenix_channel_client

Elixir Phoenix Channel Client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Phoenix.Channel.Client

Work In Progress!

  • client, channel and push API
  • socket reconnects
  • message buffering

Channel client for connecting to Phoenix

Usage

Add phoenix_channel_client as a dependency in your mix.exs file.

def deps do
  [{:phoenix_channel_client, "~> 0.0.1"} ]
end

Example usage

Socket

Using the Phoenix Channel Client requires you add a Socket module to your supervision tree for handling the socket connection.

defmodule MySocket do
  use Phoenix.Channel.Client.Socket, otp_app: :my_app
end
config :my_app, MySocket,
  url: "ws://localhost:4000/socket/websocket"

Channels function with callbacks inside a module

defmodule MyChannel do
  use Phoenix.Channel.Client

  def handle_in("new_msg", payload, state) do
    {:noreply, state}
  end

  def handle_reply({:ok, "new_msg", resp, _ref}, state) do
    {:noreply, state}
  end
  def handle_reply({:error, "new_msg", resp, _ref} state) do
    {:noreply, state}
  end
  def handle_reply({:timeout, "new_msg", _ref} state) do
    {:noreply, state}
  end

  def handle_reply({:timeout, :join, _ref} state) do
    {:noreply, state}
  end

  def handle_close(reason, state) do
    send_after(5000, :rejoin)
    {:noreply, rejoin(state)}
  end
end

usage

alias Phoenix.Channel.Client
{:ok, socket} = MySocket.start_link
Client.channel(MyChannel, socket: MySocket, topic: "rooms:lobby")
# MyChannel.start_link(socket: MySocket, topic: "rooms:lobby")
MyChannel.join(%{})
MyChannel.leave()

push = MyChannel.push("new:message", %{})
MyChannel.cancel_push(push)

About

Elixir Phoenix Channel Client


Languages

Language:Elixir 100.0%