phoenixchannelclient v0.1.3 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")
  {:exception, error} -> raise error
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")
  {:exception, error} -> raise error
end

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

:ok = PhoenixChannelClient.leave(channel)

Link to this section 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

Link to this section Types

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

Link to this section Functions

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

Creates a channel struct.

Link to this function 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)
Link to this function ensure_loop_killed(state)
Link to this function 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
Link to this function leave(channel, timeout \\ 5000)
leave(channel, number) :: send_result

Leaves the channel.

Link to this function 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
Link to this function 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
Link to this function reconnect(socket)
reconnect(socket) :: :ok | connect_error

Reconnects to the socket.

Link to this function start(opts \\ [])
Link to this function start_link(opts \\ [])