Julep.Relay (Julep v0.1.0-rc.1)

Copy Markdown View Source

A client that relays a WebSocket to another process.

Use it where a module of callbacks is more than you need, such as a script, a test or IEx:

{:ok, client} = Julep.Relay.connect(url: "wss://example.com/ws")
:ok = Julep.send(client, {:text, "ping"})

receive do
  {Julep.Relay, ^client, {:frame, {:text, text}}} -> text
end

The owner receives:

  • {Julep.Relay, client, :connected} when the WebSocket opens.
  • {Julep.Relay, client, {:frame, frame}} for each text and binary frame.
  • {Julep.Relay, client, {:disconnected, reason}} when an attempt fails or the WebSocket closes, with a Julep.disconnect_reason/0.

To send, call Julep.send/3 or Julep.cast/2. The client stops when its owner exits.

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts a relay linked to the caller and waits for its first connection.

Starts a relay linked to the caller, which connects in the background.

Types

option()

@type option() :: Julep.option() | {:owner, pid()} | {:reconnect, boolean()}

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

connect(opts)

@spec connect([option() | {:timeout, timeout()}]) :: {:ok, pid()} | {:error, term()}

Starts a relay linked to the caller and waits for its first connection.

Takes the options of start_link/1 and returns as Julep.connect/2 does.

start_link(opts)

@spec start_link([option()]) :: GenServer.on_start()

Starts a relay linked to the caller, which connects in the background.

Options

  • :owner - the process to relay to. Defaults to the caller, so pass it when starting a relay under a supervisor.
  • :reconnect - whether to reconnect after a failed attempt or a closed WebSocket. Defaults to true.

Every other option is passed to Julep.start_link/2, except :arg, which the relay sets itself.