/enchufeweb

Enchufeweb is a websocket client library written in Elixir

Primary LanguageElixirMIT LicenseMIT

Enchufeweb

hex.pm hexdocs.pm Build Status

Enchufeweb is a websocket client library written in Elixir and based on the Erlang library websocket_client.

Installation

Add enchufeweb to your list of dependencies in mix.exs:

def deps do
[{:enchufeweb, "~> 0.2.1"}]
end

How to use it

  • Implementation of the Websocket Client
defmodule Client do
use Enchufeweb
def handle_message(data, state) do 
IO.inspect data
{:ok, state}
end
def handle_connection(_, state), do: {:ok, state}
def handle_disconnection(_, state), do: {:close, "end", state}
end
  • Connect the client and send a message
iex> {:ok, client} = Client.start_link([url: "ws://localhost:8888/websocket", ws_opts: %{conn_mode: :once}]) 
iex> Client.ws_send(client, "Hola")
  • Send a ping
iex> Client.ws_send(client, :ping)
  • Close the connection
iex> Client.ws_send(client, :close)
  • Client which will send a message just after the connection
defmodule Client do
use Enchufeweb
def handle_message(data, state) do 
IO.inspect data
{:ok, state}
end
def handle_connection(_, state), do: {:reply, "Initial message", state}
def handle_disconnection(_, state), do: {:close, "end", state}
end

Test

Run the tests.

mix test