event_rabbitmq Module
     __________________________________________________________

   Table of Contents

   1. Admin Guide

        1.1. Overview
        1.2. RabbitMQ events syntax
        1.3. RabbitMQ socket syntax
        1.4. Dependencies

              1.4.1. OpenSIPS Modules
              1.4.2. External Libraries or Applications

        1.5. Exported Parameters

              1.5.1. heartbeat (integer)
              1.5.2. connect_timeout (integer)
              1.5.3. use_tls (integer)
              1.5.4. timeout (integer)

        1.6. Exported Functions
        1.7. Example
        1.8. Installation and Running

              1.8.1. OpenSIPS config file

   2. Frequently Asked Questions
   3. Contributors

        3.1. By Commit Statistics
        3.2. By Commit Activity

   4. Documentation

        4.1. Contributors

   List of Tables

   3.1. Top contributors by DevScore^(1), authored commits^(2) and
          lines added/removed^(3)

   3.2. Most recently active contributors^(1) to this module

   List of Examples

   1.1. Set heartbeat parameter
   1.2. Setting the connect_timeout parameter
   1.3. Set the use_tls parameter
   1.4. Set the timeout parameter
   1.5. E_PIKE_BLOCKED event
   1.6. RabbitMQ socket
   1.7. OpenSIPS config script - sample event_rabbitmq usage
   2.1. Event subscription
   2.2. Event subscription

Chapter 1. Admin Guide

1.1. Overview

   RabbitMQ (http://www.rabbitmq.com/) is an open source messaging
   server. It's purpose is to manage received messages in queues,
   taking advantage of the flexible AMQP protocol.

   This module provides the implementation of a RabbitMQ client
   for the Event Interface. It is used to send AMQP messages to a
   RabbitMQ server each time the Event Interface triggers an event
   subscribed for.

   The AMQP protocol is only used as the transport layer for
   notifications. The content of a message is presented in the
   next section.

1.2. RabbitMQ events syntax

   The event payload is formated as a JSON-RPC notification, with
   the event name as the method field and the event parameters as
   the params field.

1.3. RabbitMQ socket syntax

   'rabbitmq:' [user[':'password] '@' host [':' port] '/' [params
   '?'] routing_key

   Meanings:
     * 'rabbitmq:' - informs the Event Interface that the events
       sent to this subscriber should be handled by the
       event_rabbitmq module.
     * user - username used for RabbitMQ server authentication.
       The default value is 'guest'.
     * password - password used for RabbitMQ server
       authentication. The default value is 'guest'.
     * host - host name of the RabbitMQ server.
     * port - port of the RabbitMQ server. The default value is
       '5672'.
     * params - extra parameters specified as key[=value],
       separated by ';':
          + exchange - exchange of the RabbitMQ server. The
            default value is ''.
          + tls_domain - indicates which TLS domain (as defined
            using the tls_mgm module) to use for this connection.
            The use_tls module parameter must be enabled.
          + persistent - indicates that the message should be
            published as persistent delivery_mode=2. This
            parameter does not have a value.
     * routing_key - this is the routing key used by the AMQP
       protocol and it is used to identify the queue where the
       event should be sent.
       NOTE: if the queue does not exist, this module will not try
       to create it.

1.4. Dependencies

1.4.1. OpenSIPS Modules

   The following modules must be loaded before this module:
     * tls_mgm if use_tls is enabled.

1.4.2. External Libraries or Applications

   The following libraries or applications must be installed
   before running OpenSIPS with this module loaded:
     * librabbitmq-dev

1.5. Exported Parameters

1.5.1. heartbeat (integer)

   Enables heartbeat support for the AMQP communication. If the
   client does not receive a heartbeat from server within the
   specified interval, the socket is automatically closed by the
   rabbitmq-client. This prevents OpenSIPS from blocking while
   waiting for a response from a dead rabbitmq-server. The value
   represents the heartbit interval in seconds.

   Default value is “0 (disabled)”.

   Example 1.1. Set heartbeat parameter
...
modparam("event_rabbitmq", "heartbeat", 3)
...

1.5.2. connect_timeout (integer)

   The maximally allowed duration (in milliseconds) for the
   establishment of a TCP connection with a RabbitMQ server.

   Default value is “500” (milliseconds).

   Example 1.2. Setting the connect_timeout parameter
...
modparam("event_rabbitmq", "connect_timeout", 1000)
...

1.5.3. use_tls (integer)

   Setting this parameter will allow you to use TLS for broker
   connections. In order to enable TLS for a specific connection,
   you can use the "tls_domain=dom_name" parameter in the
   configuration specified through the RabbitMQ socket syntax.

   When using this parameter, you must also ensure that tls_mgm is
   loaded and properly configured. Refer to the the module for
   additional info regarding TLS client domains.

   Default value is 0 (not enabled)

   Example 1.3. Set the use_tls parameter
...
modparam("tls_mgm", "client_domain", "rmq")
modparam("tls_mgm", "certificate", "[rmq]/etc/pki/tls/certs/rmq.pem")
modparam("tls_mgm", "private_key", "[rmq]/etc/pki/tls/private/rmq.key")
modparam("tls_mgm", "ca_list",     "[rmq]/etc/pki/tls/certs/ca.pem")
...
modparam("event_rabbitmq", "use_tls", 1)
...

1.5.4. timeout (integer)

   Indicates the timeout (in milliseconds) of any command (i.e.
   publish) sent to the RabbitMQ server.

   NOTE that this parameter is available only starting with
   RabbitMQ library version 0.9.0; setting it when using an
   earlier version will have no effect, and the publish command
   will run in blocking mode.

   Default value is 0 (no timeout - blocking mode)

   Example 1.4. Set the timeout parameter
...
modparam("event_rabbitmq", "timeout", 1000) # timeout after 1s
...

1.6. Exported Functions

   No function exported to be used from configuration file.

1.7. Example

   This is an example of an event raised by the pike module when
   it decides an ip should be blocked:

   Example 1.5. E_PIKE_BLOCKED event

{
  "jsonrpc": "2.0",
  "method": "E_PIKE_BLOCKED",
  "params": {
    "ip": "192.168.2.11"
  }
}


   Example 1.6. RabbitMQ socket

        rabbitmq:guest:guest@127.0.0.1:5672/pike

        # same socket can be written as
        rabbitmq:127.0.0.1/pike

        # TLS broker connection
        rabbitmq:127.0.0.1/tls_domain=rmq?pike

1.8. Installation and Running

1.8.1. OpenSIPS config file

   This configuration file presents the usage of the
   event_rabbitmq module. In this scenario, a message is sent to a
   RabbitMQ server everytime OpenSIPS receives a MESSAGE request.
   The parameters passed to the server are the R-URI username and
   the message body.

   Example 1.7. OpenSIPS config script - sample event_rabbitmq
   usage
...
loadmodule "signaling.so"
loadmodule "sl.so"
loadmodule "tm.so"
loadmodule "rr.so"
loadmodule "maxfwd.so"
loadmodule "usrloc.so"
loadmodule "registrar.so"
loadmodule "textops.so"
loadmodule "uri.so"
loadmodule "acc.so"
loadmodule "event_rabbitmq.so"

startup_route {
        if (!subscribe_event("E_SIP_MESSAGE", "rabbitmq:127.0.0.1/sipmsg
")) {
                xlog("L_ERR","cannot the RabbitMQ server to the E_SIP_ME
SSAGE event\n");
        }
}

route{

        if (!mf_process_maxfwd_header(10)) {
                sl_send_reply(483,"Too Many Hops");
                exit;
        }

        if (has_totag()) {
                if (loose_route()) {
                        if (is_method("INVITE")) {
                                record_route();
                        }
                        route(1);
                } else {
                        if ( is_method("ACK") ) {
                                if ( t_check_trans() ) {
                                        t_relay();
                                        exit;
                                } else {
                                        exit;
                                }
                        }
                        sl_send_reply(404,"Not here");
                }
                exit;
        }

        if (is_method("CANCEL"))
        {
                if (t_check_trans())
                        t_relay();
                exit;
        }

        t_check_trans();

        if (loose_route()) {
                xlog("L_ERR",
                "Attempt to route with preloaded Route's [$fu/$tu/$ru/$c
i]");
                if (!is_method("ACK"))
                        sl_send_reply(403,"Preload Route denied");
                exit;
        }

        if (!is_method("REGISTER|MESSAGE"))
                record_route();

        if (!is_myself("$rd"))
        {
                append_hf("P-hint: outbound\r\n");
                route(1);
        }

        if (is_method("PUBLISH"))
        {
                sl_send_reply(503, "Service Unavailable");
                exit;
        }


        if (is_method("REGISTER"))
        {
                if (!save("location"))
                        sl_reply_error();

                exit;
        }

        if ($rU==NULL) {
                sl_send_reply(484,"Address Incomplete");
                exit;
        }

        if (is_method("MESSAGE")) {
                $avp(attrs) = "user";
                $avp(vals) = $rU;
                $avp(attrs) = "msg";
                $avp(vals) = $rb;
                if (!raise_event("E_SIP_MESSAGE", $avp(attrs), $avp(vals
)))
                        xlog("L_ERR", "cannot raise E_SIP_MESSAGE event\
n");
        }

        if (!lookup("location", "method-filtering")) {
                switch ($retcode) {
                        case -1:
                        case -3:
                                t_newtran();
                                t_reply(404, "Not Found");
                                exit;
                        case -2:
                                sl_send_reply(405, "Method Not Allowed")
;
                                exit;
                }
        }

        route(1);
}


route[1] {
        if (is_method("INVITE")) {
                t_on_failure("1");
        }

        if (!t_relay()) {
                sl_reply_error();
        };
        exit;
}


failure_route[1] {
        if (t_was_cancelled()) {
                exit;
        }
}

...

Chapter 2. Frequently Asked Questions

   2.1.

   What is the maximum lenght of a AMQP message?

   The maximum length of a datagram event is 16384 bytes.

   2.2.

   Where can I find more about OpenSIPS?

   Take a look at https://opensips.org/.

   2.3.

   What is the vhost used by the AMQP server?

   Currently, the only vhost supported is '/'.

   2.4.

   How can I set a vhost in the socket?

   This version doesn't support a different vhost.

   2.5.

   How can I send an event to my RabbitMQ server?

   This module acts as a transport module for the OpenSIPS Event
   Interface. Therefore, this module should follow the Event
   Interface behavior:

   The first step is to subscribe the RabbitMQ server to the
   OpenSIPS Event Interface. This can be done using the
   subscribe_event core function:

   Example 2.1. Event subscription
startup_route {
        subscribe_event("E_RABBITMQ_EVENT", "rabbitmq:127.0.0.1/queue");
}

   The next step is to raise the event from the script, using the
   raise_event core function:

   Example 2.2. Event subscription
route {
        ...
        /* decided that an event should be raised */
        raise_event("E_RABBITMQ_EVENT");
        ...
}

   NOTE that the event used above is only to exemplify the usage
   from the script. Any event published through the OpenSIPS Event
   Interface can be raised using this module.

   2.6.

   Where can I find more information about RabbitMQ?

   You can find more information about RabbitMQ on their official
   website ( http://www.rabbitmq.com/).

   2.7.

   Where can I post a question about this module?

   First at all check if your question was already answered on one
   of our mailing lists:
     * User Mailing List -
       http://lists.opensips.org/cgi-bin/mailman/listinfo/users
     * Developer Mailing List -
       http://lists.opensips.org/cgi-bin/mailman/listinfo/devel

   E-mails regarding any stable OpenSIPS release should be sent to
   <users@lists.opensips.org> and e-mails regarding development
   versions should be sent to <devel@lists.opensips.org>.

   If you want to keep the mail private, send it to
   <users@lists.opensips.org>.

   2.8.

   How can I report a bug?

   Please follow the guidelines provided at:
   https://github.com/OpenSIPS/opensips/issues.

Chapter 3. Contributors

3.1. By Commit Statistics

   Table 3.1. Top contributors by DevScore^(1), authored
   commits^(2) and lines added/removed^(3)
     Name DevScore Commits Lines ++ Lines --
   1. Razvan Crainea (@razvancrainea) 62 39 1995 310
   2. Vlad Patrascu (@rvlad-patrascu) 22 12 504 266
   3. Liviu Chircu (@liviuchircu) 9 7 23 40
   4. Ovidiu Sas (@ovidiusas) 5 3 35 7
   5. Bogdan-Andrei Iancu (@bogdan-iancu) 5 3 8 9
   6. Maksym Sobolyev (@sobomax) 5 3 7 7
   7. Peter Lemenkov (@lemenkov) 5 3 2 3
   8. Ionut Ionita (@ionutrazvanionita) 4 2 52 25
   9. franklyfox 4 2 44 5
   10. Jarrod Baumann (@jarrodb) 4 2 2 2

   All remaining contributors: Eric Tamme (@etamme), Julián Moreno
   Patiño, Vlad Paiu (@vladpaiu).

   (1) DevScore = author_commits + author_lines_added /
   (project_lines_added / project_commits) + author_lines_deleted
   / (project_lines_deleted / project_commits)

   (2) including any documentation-related commits, excluding
   merge commits. Regarding imported patches/code, we do our best
   to count the work on behalf of the proper owner, as per the
   "fix_authors" and "mod_renames" arrays in
   opensips/doc/build-contrib.sh. If you identify any
   patches/commits which do not get properly attributed to you,
   please submit a pull request which extends "fix_authors" and/or
   "mod_renames".

   (3) ignoring whitespace edits, renamed files and auto-generated
   files

3.2. By Commit Activity

   Table 3.2. Most recently active contributors^(1) to this module
                      Name                   Commit Activity
   1.  Razvan Crainea (@razvancrainea)     Sep 2011 - Mar 2024
   2.  Vlad Patrascu (@rvlad-patrascu)     Jul 2015 - Jun 2023
   3.  Liviu Chircu (@liviuchircu)         Mar 2014 - May 2023
   4.  Maksym Sobolyev (@sobomax)          Feb 2023 - Feb 2023
   5.  Peter Lemenkov (@lemenkov)          Jun 2018 - Aug 2020
   6.  Bogdan-Andrei Iancu (@bogdan-iancu) Oct 2014 - Apr 2019
   7.  Jarrod Baumann (@jarrodb)           Apr 2015 - Mar 2016
   8.  Julián Moreno Patiño                Feb 2016 - Feb 2016
   9.  Ovidiu Sas (@ovidiusas)             Jun 2015 - Jun 2015
   10. Eric Tamme (@etamme)                May 2015 - May 2015

   All remaining contributors: Ionut Ionita (@ionutrazvanionita),
   Vlad Paiu (@vladpaiu), franklyfox.

   (1) including any documentation-related commits, excluding
   merge commits

Chapter 4. Documentation

4.1. Contributors

   Last edited by: Razvan Crainea (@razvancrainea), Liviu Chircu
   (@liviuchircu), Vlad Patrascu (@rvlad-patrascu), Peter Lemenkov
   (@lemenkov), Ionut Ionita (@ionutrazvanionita).

   Documentation Copyrights:

   Copyright © 2011 www.opensips-solutions.com
