elephant v0.2.1 Elephant View Source
Elephant: A STOMP client.
Use Elephant
as the primary API and Elephant.Message
for working with received messages.
Example
{:ok, pid} = Elephant.start_link
Elephant.connect(pid, {127,0,0,1}, 61613, "admin", "admin")
callback = fn m -> IO.puts(inspect(m)) end
Elephant.subscribe(pid, "foo.bar", callback)
Elephant.disconnect(pid)
For more control, use pattern matching in the callback:
callback = fn
%Elephant.Message{command: :message, headers: headers, body: body} ->
Logger.info(["Received MESSAGE", "\nheaders: ", inspect(headers), "\nbody: ", inspect(body)])
%Elephant.Message{command: :error, headers: headers, body: body} ->
Logger.error(["Received ERROR", "\nheaders: ", inspect(headers), "\nbody: ", inspect(body)])
%Elephant.Message{command: cmd, headers: headers, body: body} ->
Logger.error([
"Received unknown command: ", cmd,
"\nheaders: ",
inspect(headers),
"\nbody: ",
inspect(body)
])
end
Starting in a supervision tree
children = [
worker(Elephant, [%{}, [name: Elephant]])
]
Link to this section Summary
Functions
Connect to server
Disconnect from server
Disconnects from server
Invoked when the server is started. start_link/3
or start/3
will
block until it returns
Receive messages from the TCP socket
Subscribe to a queue and register a callback for received messages
Unsubscribe from a queue
Link to this section Functions
Connect to server.
host
must be inet:socket_address() | inet:hostname()
, for example {127,0,0,1}
.
Disconnect from server.
Disconnects from server.
Returns {:ok, :disconnected}
or {:error, :disconnect_failed, message}
.
Invoked when the server is started. start_link/3
or start/3
will
block until it returns.
args
is the argument term (second argument) passed to start_link/3
.
Returning {:ok, state}
will cause start_link/3
to return
{:ok, pid}
and the process to enter its loop.
Returning {:ok, state, timeout}
is similar to {:ok, state}
except handle_info(:timeout, state)
will be called after timeout
milliseconds if no messages are received within the timeout.
Returning {:ok, state, :hibernate}
is similar to
{:ok, state}
except the process is hibernated before entering the loop. See
c:handle_call/3
for more information on hibernation.
Returning :ignore
will cause start_link/3
to return :ignore
and the
process will exit normally without entering the loop or calling c:terminate/2
.
If used when part of a supervision tree the parent supervisor will not fail
to start nor immediately try to restart the GenServer
. The remainder of the
supervision tree will be (re)started and so the GenServer
should not be
required by other processes. It can be started later with
Supervisor.restart_child/2
as the child specification is saved in the parent
supervisor. The main use cases for this are:
- The
GenServer
is disabled by configuration but might be enabled later. - An error occurred and it will be handled by a different mechanism than the
Supervisor
. Likely this approach involves callingSupervisor.restart_child/2
after a delay to attempt a restart.
Returning {:stop, reason}
will cause start_link/3
to return
{:error, reason}
and the process to exit with reason reason
without
entering the loop or calling c:terminate/2
.
Callback implementation for GenServer.init/1
.
Receive messages from the TCP socket.
Is called automatically when necessary. Should not be called manually.
Subscribe to a queue and register a callback for received messages.
Unsubscribe from a queue.