This part of the tutorial explain how to implement Asterisk’ Realtime functionality with MySQL.
After the brief introduction about LAMJ and the Linux + Asterisk + MySQL + Java, let’s begin to do some configuration.
I’ll not explain the general rules for Asterisk setup on a Linux box, here’s some good starting point:
Quick Start Guide (pdf)
With the file-based configuration (i.e. all the modifications done to the *.conf files) you setup a static configuration of your PBX – it may be right for you, if you don’t need to change something frequently, mostly in the dialplan.
BUT there is also the chance that you have to do it or, even more funny, you have to write a software that does this for you following the request of an end user.
So, REALTIME to the rescue.
The Realtime function (to be used inside your extensions.conf) allows you to configure your dialplan with data coming from a “RealTime repository” (that is, for me, a database); more interesting is that the modification you are doing in your database are instantly taken by Asterisk (after all, there must be a reason for the “Realtime” term).
One thing to clarify, anyway, is that you may also obtain “realtime” configuration only using Java (we will see that in later post) but the configuration will not persist (unless, after a very heavy dinner, a lot of drinks, and a sleepless night full of nightmares, you decided to programmatically edit the .conf files!!!).
Now, since the LAMJ imply the MySQL support, I will explain only this part of the configuration.
To cut a long story short, you have to:
- Have the res_config_mysql.so (it is part of the “asterisk add-ons” – don’t ask me why; explaining how to get or build it is part of a lot of other tutorials);
- Create the database and grant privileges to a given user (in this tutorial “asterisk” will be used as database name, user name, and user password: exceptionally secure, uh?! 🙂 )
- Create the dialplan table (“extensions” in this tutorial):
CREATE TABLE `extensions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`context` varchar(20) NOT NULL DEFAULT '',
`exten` varchar(20) NOT NULL DEFAULT '',
`priority` varchar(10) NOT NULL,
`app` varchar(20) NOT NULL DEFAULT '',
`appdata` varchar(128) NOT NULL DEFAULT '',
PRIMARY KEY (`context`,`exten`,`priority`),
KEY `id` (`id`)
- Edit the “res_config_mysql.conf” file with the connection parameters to the database, beside a specific [section-name] (“asterisk-section” in this tutorial)
dbhost = 127.0.0.1
dbname = asterisk
dbuser = asterisk
dbpass = asterisk
dbport = 3306
dbsock = /var/run/mysqld/mysqld.sock
dbcharset = latin1
- Add to the “extconfig.conf” file a line with the format
<family> => <realtime driver>,<[section-name]>[,<table>]
to specify that for a given configuration the given repository will be used; in our tutorial we will focus on dialplan configuration (whose family is “extensions”) with MySQL (whose realtime driver is “mysql”), like this:
extensions => mysql,asterisk-section,extensions
WARN: this is different from old versions of asterisk, where instead of the [section-name] you have to put the database name (that would have been “asterisk” in this tutorial); I can’t tell exactly when this has changed, but in 1.6 it was different;
- Add a “switch” directive in “extensions.conf” (the dialplan configuration file):
switch => Realtime/conference@extensions
please note that “conference” in the above line is the “context” to be loaded, and it must match the value you put in the “context” column inside the “extensions” table; you may have more than one switch, one for each “context” you want to be managed in realtime;
- Be sure to load the “res_config_mysql” module, and the others required for realtime to work; this can be done inside “modules.conf” file with this simple directive:
If everything is correct, when you reload or restart asterisk you should see in the console a line containing “Registered Config Engine mysql”; if not, check everything from beginning.
Using “dialplan show” in the asterisk CLI you should also have the entry:
Check your “extensions.conf” if you don’t see it.
Alt. Switch => 'Realtime/conference@extensions'
Now, as far as I can tell there is no direct way to see the “realtime” extensions from the CLI: you have to test it with a sip client. The following .sql snippet define an “echo” extension at 6878@conference:
INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "1", "Playback", "demo-echotest");
INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "2", "Echo", "");
INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "3", "Playback", "demo-echodone");
INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "4", "Hangup", "");
After inserted this records in the database (no need to reload – that’s realtime magic!) dialing “6878” from your sip client should enter you in echo test. A couple of things to keep in mind:
- “demo-echotest” and “demo-echodone” are audio files that you should have in your “sound” directory
- the sip peer should be registered with the context containing the “switch” directive, otherwise he won’t be able to access the realtime-configured context.
Well, I hope everything is clear enough for you to go on. Again, I am not diving too much in asterisk basics because the main focus of this tutorial will be hit in the following parts regarding java. Anyway, you are more than welcome to post your questions or to point me to the silly mistakes I may have done.
In the next part we will begin with actual Java coding: don’t miss it!!!