phoenixchannelclient v0.1.2 PhoenixChannelClient

Phoenix Channels Client

Example

{:ok, pid} = PhoenixChannelClient.start_link()

{:ok, socket} = PhoenixChannelClient.connect(pid,
  host: "localhost",
  path: "/socket/websocket",
  params: %{token: "something"},
  secure: false)

channel = PhoenixChannelClient.channel(socket, "room:public", %{name: "Ryo"})

case PhoenixChannelClient.join(channel) do
  {:ok, %{message: message}} -> IO.puts(message)
  {:error, %{reason: reason}} -> IO.puts(reason)
  :timeout -> IO.puts("timeout")
end

case PhoenixChannelClient.push_and_receive(channel, "search", %{query: "Elixir"}, 100) do
  {:ok, %{result: result}} -> IO.puts("#{length(result)} items")
  {:error, %{reason: reason}} -> IO.puts(reason)
  :timeout -> IO.puts("timeout")
end

receive do
  {"new_msg", message} -> IO.puts(message)
  :close -> IO.puts("closed")
  {:error, error} -> ()
end

:ok = PhoenixChannelClient.leave(channel)

Summary

Functions

Creates a channel struct

Connects to the specified websocket

Joins to the channel and subscribes messages

Leaves the channel

Pushes a message and receives a reply

Reconnects to the socket

Types

channel()
channel() :: %PhoenixChannelClient.Channel{params: term, socket: term, topic: term}
connect_error()
connect_error() :: {:error, term}
error_result()
error_result() :: {:error, term}
ok_result()
ok_result() :: {:ok, term}
result()
send_result()
send_result() :: :ok | {:error, term}
socket()
socket() :: %PhoenixChannelClient.Socket{server_name: term}
subscription()
subscription() :: %PhoenixChannelClient.Subscription{mapper: term, matcher: term, name: term, pid: term}
timeout_result()
timeout_result() :: :timeout

Functions

channel(socket, topic, params \\ %{})
channel(socket, String.t, map) :: channel

Creates a channel struct.

connect(name, opts)
connect(term, keyword) :: {:ok, socket} | connect_error

Connects to the specified websocket.

Options

  • :host
  • :port optional
  • :path optional, “/“ by default
  • :params optional, %{} by default
  • :secure optional, false by default

Example

PhoenixChannelClient.connect(pid,
  host: "localhost",
  path: "/socket/websocket",
  params: %{token: "something"},
  secure: false)
ensure_loop_killed(state)
join(channel, timeout \\ 5000)
join(channel, number) :: result

Joins to the channel and subscribes messages.

Receives {event, payload} or :close.

Example

case PhoenixChannelClient.join(channel) do
  {:ok, %{message: message}} -> IO.puts(message)
  {:error, %{reason: reason}} -> IO.puts(reason)
  :timeout -> IO.puts("timeout")
end
receive do
  {"new_msg", message} -> IO.puts(message)
  :close -> IO.puts("closed")
  {:error, error} -> ()
end
leave(channel, timeout \\ 5000)
leave(channel, number) :: send_result

Leaves the channel.

push(channel, event, payload)
push(channel, String.t, map) :: send_result

Pushes a message.

Example

case PhoenixChannelClient.push(channel, "new_msg", %{text: "Hello"}, 100) do
  :ok -> ()
  {:error, term} -> IO.puts("failed")
end
push_and_receive(channel, event, payload, timeout \\ 5000)
push_and_receive(channel, String.t, map, number) :: result

Pushes a message and receives a reply.

Example

case PhoenixChannelClient.push_and_receive(channel, "search", %{query: "Elixir"}, 100) do
  {:ok, %{result: result}} -> IO.puts("#{length(result)} items")
  {:error, %{reason: reason}} -> IO.puts(reason)
  :timeout -> IO.puts("timeout")
end
reconnect(socket)
reconnect(socket) :: :ok | connect_error

Reconnects to the socket.

start(opts \\ [])
start_link(opts \\ [])