Gundam

Reliable elixir websocket client powered by Gun 2.0 (erlang)

Example of usage

defmodule MyWebsocket do
  use Gundam.Websocket
end

{:ok, pid} = MyWebsocket.start_link(url: "ws://localhost/websocket")

It can be used also the callbacks:

  @doc """
  trigger when the websocket client connects successfully
  """
  @callback on_connect(headers(), state()) :: state()

  @doc """
  trigger when the connection is lost (gun will try to connect again and upgrade to ws)
  """
  @callback on_disconnect(reason(), state()) :: state()

  @doc """
  trigger when the websocket client fails to connect successfully
  """
  @callback on_close(code(), state()) :: state()

  @doc """
  trigger when the websocket client has abruptly an error
  """
  @callback on_error(reason(), state()) :: state()

  @doc """
  trigger when the websocket client recieves an message from the server
  """
  @callback on_message(message(), state()) :: state()

Using on_connect/2 callback


defmodule MyWebsocket do
  use Gundam.Websocket
  
  @impl Gundam.Websocket
  def on_connect(_headers, state) do
    # Doing something awesome ...
    state
  end
end

Installation

If available in Hex, the package can be installed by adding gundam to your list of dependencies in mix.exs:

def deps do
  [
    {:gundam, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/gundam.