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
Pushes a message and receives a reply
Reconnects to the socket
Types
Functions
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)
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
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.