Exampple.Client (exampple v0.10.6)

Client is a simple state machine inside of a process which helps us to create a client connection to a XMPP Server. The dynamic of the connection is contained inside of the client when we perform the connection and then we can send different kind of stanzas to the server.

The aim of this Client is to create a simple way to test the system but you can use it even to create bots based on XMPP.

It has the possibility to register templates and returns back the stanzas which are being received from the XMPP server to the calling process. This way we are responsible of the communication.

Link to this section Summary

Functions

Add hook letting us to run a specific code for a received stanza. The hooks should be anonymous functions in the way

Returns a specification to start this module under a supervisor.

Send a message to the client process to start the connection. Optionally, we can specify the name of the process where to connect, by default this is the value provided by __MODULE__.

Send a message to the client process identified by name as PID or registered process to stop the connection. The name parameter is optional, by default it's set to __MODULE__.

Ask to the process about whether the connection is active. The checks are based on the process relanted to the passed name (it must be a registered name), the PID should be valid and alive, and then we ask to the client if it's connected. If no parameter is provided it is __MODULE__.

Send information (stanzas) via the client process directly to the XMPP Server. These stanzas could be sent to whoever inside of the XMPP network locally or even to other domains if the network is federated.

Starts the client. We can send a name to be registered or use the module name (__MODULE__) by default. As args the system requires a Map with the information for the connection. The information we can provide is

Stops the process. You can specify the name of the process to be stopped, by default this value is set as __MODULE__.

Add process as a tracer for the current XMPP client. It let us receive all of the events happening inside of the client. We can have as many tracers as needed.

Upgrade a connection as TLS.

Wait until the system is connected to start processing messages. This is in use for functional tests and could be used as a phase in the start of the applications.

Link to this section Types

Link to this type

hook_function()

Specs

hook_function() ::
  (Exampple.Router.Conn.t() -> {:ok, Exampple.Router.Conn.t()} | :drop)

Specs

hook_name() :: String.t()

Link to this section Functions

Link to this function

add_hook(name \\ __MODULE__, hook_name, hook_function)

Specs

add_hook(GenServer.server(), hook_name(), hook_function()) :: :ok

Add hook letting us to run a specific code for a received stanza. The hooks should be anonymous functions in the way:

fn conn ->
  if conn.stanza_type == "iq" and conn.type == "result" do
    :drop
  else
    {:ok, conn}
  end
end

These functions let us stop processing of the stanzas or add specific listeners for an incoming stanza we are awaiting for. For example, if we want to get the stanza in our process:

parent = self()
f = fn conn ->
  if conn.stanza_type == "iq" and conn.type == "error" do
    send(parent, {:stanza_error, conn.stanza})
  end
  {:ok, conn}
end
Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

connect(name \\ __MODULE__)

Specs

connect(atom() | pid()) :: :ok

Send a message to the client process to start the connection. Optionally, we can specify the name of the process where to connect, by default this is the value provided by __MODULE__.

Link to this function

del_hook(name \\ __MODULE__, hook_name)

Specs

del_hook(GenServer.server(), hook_name()) :: :ok
Link to this function

disconnect(name \\ __MODULE__)

Specs

disconnect(atom() | pid()) :: :ok

Send a message to the client process identified by name as PID or registered process to stop the connection. The name parameter is optional, by default it's set to __MODULE__.

Link to this function

is_connected?(name \\ __MODULE__)

Specs

is_connected?(atom() | pid()) :: boolean()

Ask to the process about whether the connection is active. The checks are based on the process relanted to the passed name (it must be a registered name), the PID should be valid and alive, and then we ask to the client if it's connected. If no parameter is provided it is __MODULE__.

Link to this function

send(data_or_conn, name \\ __MODULE__)

Specs

Send information (stanzas) via the client process directly to the XMPP Server. These stanzas could be sent to whoever inside of the XMPP network locally or even to other domains if the network is federated.

The accepted paramter data_or_conn could be a string (or binary) or a Conn struct. Optionally, you can specify a second parameter as the name of the registered process. The default value for the name paramter is __MODULE__.

Example:

iex> Exampple.Client.send("<presence/>")
:ok
Link to this function

start_link(name \\ __MODULE__, args)

Specs

start_link(atom() | pid(), map()) :: GenStateMachine.on_start()

Starts the client. We can send a name to be registered or use the module name (__MODULE__) by default. As args the system requires a Map with the information for the connection. The information we can provide is:

  • host: the hostname or IP where we will be connected.
  • port: the port number where we will be connected.
  • domain: the XMPP domain we are going to use.
  • trimmed: if the stanzas will be trimmed (default false).
  • set_from: if we are going to set the from for each stanza written by the client (default false).
  • ping: if we active the ping. We can set here the number of milliseconds to send the ping or false to disable it (default false).
  • tcp_handler: the module which we will use to handle the connection (default Exampple.Tcp).
  • tls_handler: the module which we will use to handle the TLS connection, if any (default Exampple.Tls).
  • trace: indicate if you want to receive trace output from the client.
Link to this function

stop(name \\ __MODULE__)

Specs

stop(atom() | pid()) :: :ok

Stops the process. You can specify the name of the process to be stopped, by default this value is set as __MODULE__.

Link to this function

trace(name \\ __MODULE__, enable?)

Specs

trace(GenServer.server(), enable? :: boolean()) :: :ok

Add process as a tracer for the current XMPP client. It let us receive all of the events happening inside of the client. We can have as many tracers as needed.

Link to this function

upgrade_tls(name \\ __MODULE__)

Specs

upgrade_tls(atom() | pid()) :: :ok

Upgrade a connection as TLS.

Link to this function

wait_for_connected(name \\ __MODULE__, sleep \\ 250, retries \\ 2)

Wait until the system is connected to start processing messages. This is in use for functional tests and could be used as a phase in the start of the applications.