RabbitMQ support in FreeSWITCH

Intro to RabbitMQ

RabbitMQ is a message handler that routes messages through exchanges, then sends them to queues, where the messages can be pulled from the queue and processed in whatever measure the administrator decides.

mod_amqp in FreeSWITCH

The FreeSWITCH module for RabbitMQ, mod_amqp (Advanced Message Queuing Protocol), provides the connective tissue between FreeSWITCH and RabbitMQ. The preferred setup is to have RabbitMQ installed on a separate server from FreeSWITCH, though they can both be run on the same server for small-scale environments or for testing, with both servers running Debian Jessie.

Installing Pre-Requisites

On the Debian Jessie servers, install your text editor of choice. Ours is currently emacs.

 apt-get install emacs-nox -y

Installing RabbitMQ

Add the rabbitmq source to your Debian sources list.

 emacs /etc/apt/sources.list

 # Add this line:
 deb http://www.rabbitmq.com/debian/ testing main

Copy over the public key.

 wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | apt-key add -

Update to apply, then install the rabbit-mq server packages.

 apt-get update
 apt-get install rabbitmq-server libanyevent-rabbitmq-perl -y

Copy the example configs into a configuration file.

 zcat /usr/share/doc/rabbitmq-server/rabbitmq.config.example.gz > /etc/rabbitmq/rabbitmq.config

Navigate to the rabbitmq directory, then open the rabbitmq configuration file and enable loopback_users.

 cd /etc/rabbitmq
 emacs ./rabbitmq.config

 # change this:
 %% {loopback_users, []},

 # to this:
 {loopback_users, []}

Enable the plugin management.

 rabbitmq-plugins enable rabbitmq_management

Start the rabbitmq server.

 service rabbitmq-server restart

You can now open RabbitMQ in your browser by going to :15672 and logging in with guest guest.

Install FreeSWITCH from Packages

The following steps are based on the FreeSWITCH page.

 apt-get update && apt-get install -y curl
 curl http://files.freeswitch.org/repo/deb/debian/freeswitch_archive_g0.pub | apt-key add -

 echo "deb http://files.freeswitch.org/repo/deb/freeswitch-1.6/ jessie main" > /etc/apt/sources.list.d/freeswitch.list
 echo "deb http://files.freeswitch.org/repo/deb/debian-unstable/ jessie main" >> /etc/apt/sources.list.d/freeswitch.list

 apt-get update && apt-get install -y freeswitch-meta-all

Start FreeSWITCH in the background with

  service freeswitch restart

Setting up mod_amqp

Navigate to the FreeSWITCH directory, and configure to load mod_amqp

 cd /etc/freeswitch
 emacs ./autoload_configs/modules.conf.xml
      <load module="mod_amqp"/>

Update the amqp connection configuration.

 emacs ./autoload_configs/amqp.conf.xml

Start by editing the connections in the default profile. Update the primary connection hostname to your RabbitMQ server IP, and change the primary port from 5673 to the port set on the RabbitMQ server, most likely 5672. If you changed the username and password in RabbitMQ, update it here as well. If there is no secondary server, comment out or remove that connection.

 <connection name="primary">
   <param name="hostname" value="localhost"/>
   <param name="virtualhost" value="/"/>
   <param name="username" value="guest"/>
   <param name="password" value="guest"/>
   <param name="port" value="5672"/>
   <param name="heartbeat" value="0"/>
 </connection>

You can also specifiy which FreeSWITH events to send to RabbitMQ. The default filter grabs channel create, channel destroy, heartbeat, and dtmf. If you prefer all events, that option is present but commented out. Simply uncomment the SWITCH_EVENT_ALL line and comment out the default. If you know the name of a specific event you want to track, you can add it to the comma separated list.

 <!--    <param name="event_filter" value="SWITCH_EVENT_ALL"/> -->
 <param name="event_filter" value="SWITCH_EVENT_CHANNEL_CREATE,SWITCH_EVENT_CHANNEL_DESTROY,SWITCH_EVENT_HEARTBEAT,SWITCH_EVENT_DTMF"/>

Set the command connection details and binding_key in the commands configuration:

 <commands>
   <profile name="default">
     <connections>
       <connection name="primary">
         <param name="hostname" value="localhost"/>
         <param name="virtualhost" value="/"/>
         <param name="username" value="guest"/>
         <param name="password" value="guest"/>
         <param name="port" value="5672"/>
         <param name="heartbeat" value="0"/>
       </connection>
     </connections>
     <params>
       <param name="exchange-name" value="TAP.Commands"/>
       <param name="binding_key" value="your_server_key"/>
       <param name="reconnect_interval_ms" value="1000"/>
     </params>
   </profile>
 </commands>

Reload mod_amqp to apply the changes

 fs_cli

 reloadxml
 reload mod_amqp

Using mod_amqp

Now that FreeSWITCH and RabbitMQ are configured with mod_amqp, you will need a script to grab event messages. AnyEvent::RabbitMQ provides a straightforward method of interacting asynchronously with the RabbitMQ server. You can create the perl script wherever you would like.

The example script below shows how to bind to a queue and consume the events.

amqp_client.pl

The example script below shows how to run a FreeSWITCH command, with $ARGV[0] being the binding_key as set in autoload_configs/amqp.conf.xml commands params and $ARGV[1] being the command for FreeSWITCH to run.

amqp_command.pl

Conclusion

This is a basic introduction for using RabbitMQ with FreeSWITCH through mod_amqp. If you have any questions or comments, feel free to submit a comment below.

Category
Tagcloud
Powered by Pelican
which takes great advantage of Python