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
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
subscription()
subscription() :: %PhoenixChannelClient.Subscription{mapper: term, matcher: term, name: term, pid: term}
Link to this section Functions
Link to this function
channel(socket, topic, params \\ %{})
Creates a channel struct.
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)
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
Leaves the channel.
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)
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
Reconnects to the socket.
Link to this function
start(opts \\ [])
Link to this function
start_link(opts \\ [])