Work In Progress!
- client, channel and push API
- socket reconnects
- message buffering
Channel client for connecting to Phoenix
Add phoenix_channel_client as a dependency in your mix.exs
file.
def deps do
[
{:phoenix_channel_client, "~> 0.0.1"},
{:poison, "~> 2.0"} #optional. You can use your own JSON serializer
]
end
Example usage
Using the Phoenix Channel Client requires you add a Socket module to your supervision tree for handling the socket connection.
defmodule MySocket do
use PhoenixChannelClient.Socket, otp_app: :my_app
end
config :my_app, MySocket,
url: "ws://localhost:4000/socket/websocket",
serializer: Poison
Channels function with callbacks inside a module
defmodule MyChannel do
use PhoenixChannelClient
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
{:ok, socket} = MySocket.start_link
{:ok, channel} = PhoenixChannelClient.channel(MyChannel, socket: MySocket, topic: "rooms:lobby")
MyChannel.join(channel, %{})
MyChannel.leave(channel)
push = MyChannel.push("new:message", %{})
MyChannel.cancel_push(push)