# &adminguide;

## Overview

The DMQ module implements a distributed message queue on top of Kamailio in order to enable the passing/replication of data between multiple instances. The DMQ "nodes" within the system are grouped in a logical entity called the DMQ "bus" and are able to communicate with each other by sending/receiving messages (either by broadcast or directly to a specific node). The system transparently deals with node discovery, consistency, retransmissions, etc.

Other entities ("peers") are then able to utilize the DMQ bus to pass messages between themselves. Peers are grouped by name in order to ensure the correct messages are passed to the relevant peers. This grouping of peers can be compared to a topic in a typical pub/sub system.

DMQ sends SIP requests using the KDMQ request method, that is private to Kamailio.

### Example of a KDMQ message

This message is for basic DMQ bus handling. Other messages are produced by other modules, like the htable module.

    KDMQ sip:notification_peer@192.168.40.15:5090 SIP/2.0
    Via: SIP/2.0/UDP 192.168.40.15;branch=z9hG4bK55e5.423d95110000
    To: <sip:notification_peer@192.168.40.15:5090>
    From: <sip:notification_peer@192.168.40.15:5060>;tag=2cdb7a33a7f21abb98fd3a44968e3ffd-5b01
    CSeq: 10 KDMQ
    Call-ID: 1fe138e07b5d0a7a-50419@192.168.40.15
    Content-Length: 116
    User-Agent: kamailio (4.3.0 (x86_64/linneaus))
    Max-Forwards: 1
    Content-Type: text/plain

    sip:192.168.40.16:5060;status=active
    sip:192.168.40.15:5060;status=disabled
    sip:192.168.40.17:5060;status=active

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- sl.

- tm.

### External Libraries or Applications

- The DMQ module itself has no external dependencies. However, each peer will need to use its own (de)serialization mechanism. Some examples are libtpl, protobuf. .

## Parameters

### `server_address`(str)

The local server address. This is the interface over which the DMQ engine will send/receive messages.

Default value is NULL.

#### Set `server_address` parameter

    ...
    modparam("dmq", "server_address", "sip:10.0.0.20:5060")
    ...

### `notification_address`(str)

The address of another DMQ node from which the local node should retrieve initial information about all other nodes.

Default value is NULL.

#### Set `notification_address` parameter

    ...
    modparam("dmq", "notification_address", "sip:10.0.0.21:5060")
    ...

### `multi_notify`(int)

Enables the ability to resolve multiple IPv4/IPv6 addresses for a single notification address.

A value of zero resolves to the first IP address found. A non-zero value resolves to all IP addresses associated with the host. This includes addresses from DNS SRV records, A and AAAA records.

Default value is 0.

#### Set `multi_notify` parameter

    ...
    modparam("dmq", "multi_notify", 1)
    ...

### `num_workers`(int)

The number of worker threads for sending/receiving messages.

Default value is 2.

#### Set `num_workers` parameter

    ...
    modparam("dmq", "num_workers", 4)
    ...

### `worker_usleep`(int)

The default locking/synchronisation mechanism between producer/consumer threads is the optimum for most environments. On some systems (e.g. FreeBSD) it can cause high CPU load and in such cases, it can be useful to disable locking and switch to polling for tasks at set intervals instead - putting the thread to sleep in-between and taking it out of process during that time.

A value >0 will disable the default locking and set the polling interval (in microseconds), which can be tuned to suit the specific environment.

Default value is 0 (recommended for most systems).

#### Set `worker_usleep` parameter

    ...
    modparam("dmq", "worker_usleep", 1000)
    ...

### `ping_interval`(int)

The number of seconds between node pings (for checking status of other nodes).

Minimum value is 60 (default).

#### Set `ping_interval` parameter

    ...
    modparam("dmq", "ping_interval", 90)
    ...

## Functions

### `dmq_handle_message([continue])`

Handles a DMQ message by passing it to the appropriate local peer (module). The peer is identified by the user part of the To header.

Meaning of parameters:

- continue - by default, dmq_handle_message() will end execution of routing script. If this optional parameter is set to "1", dmq_handle_message() will continue executing the routing script after it's been called.

This function can be used from REQUEST_ROUTE.

#### `dmq_handle_message` usage

    ...
            if(is_method("KDMQ"))
            {
                    dmq_handle_message();
            }
    ...

### `dmq_send_message(peer, node, body, content_type)`

Sends a DMQ message directly from config file to a single node.

Meaning of parameters:

- peer - name of peer that should handle the message.

- node - the node to which the message should be sent.

- body - the message body.

- content_type - the MIME type of the message body.

This function can be used from any route.

#### `dmq_send_message` usage

    ...
    	dmq_send_message("peer_name", "sip:10.0.0.21:5060", "Message body...", "text/plain");
    ...

### `dmq_bcast_message(peer, body, content_type)`

Broadcasts a DMQ message from config file to all active nodes (except self).

Meaning of parameters:

- peer - name of peer that should handle the message.

- body - the message body.

- content_type - the MIME type of the message body.

This function can be used from any route.

#### `dmq_bcast_message` usage

    ...
            dmq_bcast_message("peer_name", "Message body...", "text/plain");
    ...

### `dmq_t_replicate([skip_loop_test])`

Replicates the current SIP message to all active nodes (except self). Useful for replicating REGISTER, PUBLISH etc. in a clustered environment.

Meaning of parameters:

- skip_loop_test - by default, DMQ checks the source IP of the message prior to replication, to ensure it has not been sent by another DMQ node (to avoid infinite loops). If this optional parameter is set to "1", the loop test is not performed. This makes sense, from a performance perspective, if you have already performed the necessary checks in the config script (see dmq_is_from_node()).

This function can be used from REQUEST_ROUTE only.

#### `dmq_t_replicate` usage

    ...
            dmq_t_replicate();
    ...

### `dmq_is_from_node()`

Checks whether the current message has been sent by another DMQ node in the cluster.

This function can be used from REQUEST_ROUTE only.

#### `dmq_is_from_node` usage

    ...
    	# Example REGISTER block
            if (dmq_is_from_node()) {
    		# Already authenticated, just save contact...
    	} else {
    		# Authenticate, save contact etc.
    		# Assuming all successful...
    		dmq_t_replicate("1"); # Already checked source, don't perform loop test again
    	}
    ...

## RPC Commands

### dmq.list_nodes

List the DMQ nodes. It has no parameters.

#### dmq.list_nodes usage

    ...
    &kamcmd; dmq.list_nodes
    ...