JAXL library – List of available hooks for various XMPP events

Jaxl 2.x provides an event mechanism using which developers can register callbacks for various xmpp events inside their application code. This blog post will demonstrate how to register callbacks for required xmpp events and go through a list of all available hooks. Finally, we will discuss parameters that are passed to called back methods by Jaxl core.

Registering callback on XMPP events
Applications can register callback for various XMPP events. Jaxl core will then callback application methods (with 2 parameters) every time associated XMPP event occurs. Shown below are some sample examples for registering callbacks.

When application callback’d method is a function:

function postAuth($payload, $jaxl) {

$jaxl->addPlugin('jaxl_post_auth', 'postAuth');

When application callback’d method is a public static method of a class:

class MyXMPPApp {
    public static function postAuth($payload, $jaxl) {

$jaxl->addPlugin('jaxl_post_auth', array('MyXMPPApp', 'postAuth'));

When application callback’d method is a public method inside a class:

class MyXMPPApp {
    function postAuth($payload, $jaxl) {

$MyXMPPApp = new MyXMPPApp();
$jaxl->addPlugin('jaxl_post_auth', array($MyXMPPApp, 'postAuth'));

In all the above examples jaxl_post_auth is one of the available hook for registering callbacks.

List of available hooks
Below is a complete list of available hooks in order of their occurrence within a Jaxl instance life cycle:

Hooks for events related to instance connection and authentication steps in various modes:

  • jaxl_post_connect
  • jaxl_get_auth_mech
  • jaxl_get_facebook_key
  • jaxl_post_auth_failure
  • jaxl_post_auth
  • jaxl_post_handshake
  • jaxl_pre_shutdown
  • jaxl_post_disconnect
  • jaxl_get_empty_body

Hooks for events related to XMPP stream and stanza’s:

  • jaxl_get_stream_error
  • jaxl_get_presence
  • jaxl_get_message
  • jaxl_get_iq_get
  • jaxl_get_iq_set
  • jaxl_get_iq_error
  • jaxl_send_message
  • jaxl_send_presence

Hooks for events related to reading/writing of XMPP packets and internal packet routing:

  • jaxl_get_xml
  • jaxl_send_xml
  • jaxl_send_body
  • jaxl_pre_handler
  • jaxl_post_handler

TO-DO: Update when every hook is called inside your application life cycle and list of parameters passed for each callback. As of now you can var_dump($payload); inside your callback method.

How to write External Jabber Components in PHP using Jaxl library?

Jabber Component Protocol (XEP-0114) documents how XMPP protocol can be used to communicate between servers and “external” components over the Jabber network. XMPP components “bind” to a domain, usually a sub-domain of the main XMPP service, such as service.example.org.

All incoming stanzas addressed to that domain (to='service.example.org') or to entities on that domain ([email protected]') will be routed to your Jaxl (Jabber XMPP Library) based code. In this blog post, I will demonstrate a sample external jabber component bot written in PHP using Jaxl library.

Refer Jaxl Installation, Usage guide and Example apps if you are new to Jaxl. Demonstrated component bot code can be obtained from [email protected].

Using Jabber Component Protocol
Include Jaxl implementation of XEP-0114 in your application code to setup necessary environment for using Jabber component protocol. Here is how this can be done at the top of your application code:

        // Initialize Jaxl Library
        $jaxl = new JAXL(array(
                'component' => JAXL_COMPONENT_HOST,
                'port' => JAXL_COMPONENT_PORT

        // Include required XEP's
        jaxl_require('JAXL0114', $jaxl); // Jabber Component Protocol

Register callback for XMPP events
Above we have setup the necessary environment for writing external Jabber component bots. Next we register callback for necessary XMPP events inside our componentbot class.

        // Sample Component class
        class componentbot {


        // Add callbacks on various event handlers
        $componentbot = new componentbot();
        JAXLPlugin::add('jaxl_pre_handshake', array($componentbot, 'doAuth'));
        JAXLPlugin::add('jaxl_post_handshake', array($componentbot, 'postAuth'));
        JAXLPlugin::add('jaxl_get_message', array($componentbot, 'getMessage'));

Component bot class
Finally, lets complete the missing pieces inside componentbot class.

        // Sample Component class
        class componentbot {

                function doAuth() {
                        $jaxl->log("Going for component handshake ...", 1);
                        return JAXL_COMPONENT_PASS;

                function postAuth() {
                        $jaxl->log("Component handshake completed ...", 1);

                function getMessage($payloads) {
                        global $jaxl;

                        // echo back
                        foreach($payloads as $payload) {
                                $jaxl->sendMessage($payload['from'], $payload['body'], $payload['to']);


Configure, Setup and Run
If you have a local “ejabberd” installed, add following lines inside ejabberd.cfg to make example component bot to work:

  {5559, ejabberd_service, [
                          {host, "component.localhost", [{password, "pass"}]}

Update jaxl.ini if you choose to have different password, port or host name above:

        // Connecting jabber server details
        define('JAXL_HOST_NAME', 'localhost');
        define('JAXL_HOST_DOMAIN', 'localhost');

        // Component bot setting
        define('JAXL_COMPONENT_HOST', 'component.'.JAXL_HOST_DOMAIN);
        define('JAXL_COMPONENT_PASS', 'pass');
        define('JAXL_COMPONENT_PORT', 5559);

Finally, run from command line:

[email protected]:~/usr/share/php/jaxl/app/componentbot# jaxl componentbot.php
[15008] 2010-08-24 01:40:03 - Socket opened to the jabber host localhost:5559 ...

Tail jaxl.log for details:

[15008] 2010-08-24 01:40:04 - Going for component handshake ...

[15008] 2010-08-24 01:40:04 - [[XMPPSend]] 63

[15008] 2010-08-24 01:40:05 - [[XMPPGet]]

[15008] 2010-08-24 01:40:05 - Component handshake completed ...

Log into your Ejabberd with a client and send a message to [email protected] – You should receive an instant response back – congratulations!

XEP 0045 – Multi User Chat (MUC) available methods in Jaxl 2.0

Jaxl 2.0.3 (Jabber XMPP Library in PHP) comes with 15 XMPP extensions including XEP-0045 a.k.a. Multi-User Chat (Conference Room). In this blog post, we will go through all the methods available for XMPP applications developed using Jaxl library.

Using MUC methods
You need to include Jaxl implementation of XEP-0045 in your application code to start using the methods listed below. Refer Jaxl Installation, Usage guide and Example apps if you are new to Jaxl. Here is how this can be done at the top of your application code:

// initialize Jaxl instance
$jaxl = new JAXL();

// include MUC XEP
jaxl_require('JAXL0045', $jaxl); // or simply $jaxl->requires('JAXL0045');

Multi-User Chat available methods:
Below is a detailed list of methods from multi-user room extension:

  • $jaxl->JAXL0045(‘joinRoom’, $jid, $roomJid.”/”.$nick, $history=0, $type=’seconds’)
  • $jaxl->JAXL0045(‘exitRoom’, $jid, $roomJid)
  • $jaxl->JAXL0045(‘kickOccupant’, $fromJid, $nick, $roomJid, $reason=FALSE, $callback=FALSE)
  • $jaxl->JAXL0045(‘getRoomConfig’, $jid, $roomJid, $callback=FALSE)
  • $jaxl->JAXL0045(‘setRoomConfig’, $jid, $roomJid, $fields, $callback=FALSE)
  • $jaxl->JAXL0045(‘grantOwnerPrivileges’, $fromJid, $toJid, $roomJid, $reason=FALSE, $callback=FALSE)
  • $jaxl->JAXL0045(‘revokeOwnerPrivileges’, $fromJid, $toJid, $roomJid, $reason=FALSE, $callback=FALSE)
  • $jaxl->JAXL0045(‘grantAdminPrivileges’, $fromJid, $toJid, $roomJid, $reason=FALSE, $callback=FALSE)
  • $jaxl->JAXL0045(‘removeAdminPrivileges’, $fromJid, $toJid, $roomJid, $reason=FALSE, $callback=FALSE)

All parameters are mandatory to be passed while calling the above available methods. To be continued as more method gets added like banUser, destroyRoom, etc.

PHP Code, Setup and Demo of Jaxl boshchat application

Jaxl 2.0 bosh support allow web developers to write real time web applications within minutes, without having any pre-requisite knowledge about the XMPP protocol itself. In this blog post, I will walk you through setup and demo of an XMPP based web chat application using Jaxl library.

Get the code
Follow the following steps to download and install this sample web application on your systems:

  • Clone the development branch of Jaxl library
    [email protected]:~/git# git clone [email protected]:abhinavsingh/JAXL.git
    [email protected]:~/git# cd JAXL/
    [email protected]:~/git/JAXL#

    If you are not familiar with git, simply visit [email protected], click Download Source and extract under ~/git/JAXL directory on your system

  • Once inside Jaxl source directory, build the latest development package
    [email protected]:~/git/JAXL# ./build.sh
  • Install Jaxl library (view installation detail and options)
    [email protected]:~/git/JAXL# ./build.sh install
    uninstalling old package...

Setup web chat application
Jaxl library is default installed under /usr/share/php/jaxl folder. Application code for our web chat application can be found under /usr/share/php/jaxl/app/boshchat folder.

Follow these steps to setup web chat application on your system:

  • I assume you have http://localhost/ configured on your local web server and it runs out of /var/www folder. Create following symlinks:
    [email protected]:~/git/JAXL# cd /var/www
    [email protected]:/var/www# ln -s /usr/share/php/jaxl/app/boshchat/boshchat.php index.php
    [email protected]:/var/www# ln -s /usr/share/php/jaxl/app/boshchat/jaxl.ini jaxl.ini
    [email protected]:/var/www# ln -s /usr/share/php/jaxl/env/jaxl.js jaxl.js
    [email protected]:/var/www# ln -s /usr/bin/jaxl jaxl.php

    Edit/Remove #!/usr/bin/env php inside jaxl.php if it causes any problem on your system.

  • Open and edit jaxl.ini
    define('JAXL_BOSH_COOKIE_DOMAIN', false);
  • I assume you have access to XMPP over Bosh enabled jabber server. Ejabberd users can verify this by hitting http://localhost:5280/http-bind in the browser
  • Open and edit index.php
    define('BOSHCHAT_ADMIN_JID', [email protected]');

    All messages sent using this web chat application will be routed to BOSHCHAT_ADMIN_JID

Ready for the demo
To run this example web chat application, visit http://localhost in your browser window. Enter a username/password already registered on your jabber server and press connect.

Login as BOSHCHAT_ADMIN_JID using a desktop client, so that you can receive messages sent from the browser on your desktop client.

Below is a screenshot when I logged in as “abhinavsingh” from browser and BOSHCHAT_ADMIN_JID was set to “[email protected]”:

Facebook chat connect with X-FACEBOOK-PLATFORM using Jaxl 2.0

Facebook chat provides two authentication mechanisms for authenticating chat client users. DIGEST-MD5 require chat client users to enter their username and password, while X-FACEBOOK-PLATFORM can be used to provide better user experience by using simple Facebook Platform authentication. In this blog post, I will demonstrate how to use Jaxl library for X-FACEBOOK-PLATFORM authentication.

Setup Jaxl library on your system and edit packaged sample echobot application with facebook user account details. Alternately you can also specify connecting user details inside jaxl.ini configuration file.

        $jaxl = new JAXL(array(
                'pass'=>'', // Not required, we will use user session key instead

Add callback for hook jaxl_get_facebook_key:

JAXLPlugin::add('jaxl_get_facebook_key', array($echobot, 'getFacebookKey'));

Complete getFacebookKey method inside echobot application, which should return back following key information:

function getFacebookKey() {
                        return array(
                                '', // Your application secret key
                                '', // Your application api key
                                '' // Connecting user session key

Update doAuth method to use X-FACEBOOK-PLATFORM auth mechanism:

                function doAuth($mechanism) {
                        global $jaxl;

Finally, run echobot from command line:

[email protected]:/usr/share/php/jaxl/app/echobot# jaxl echobot.php
[1942] 2010-08-08 05:35:10 - Socket opened to the jabber host chat.facebook.com:5222 ...
[1942] 2010-08-08 05:35:11 - Performing Auth type: X-FACEBOOK-PLATFORM
[1942] 2010-08-08 05:35:26 - Auth completed...

XEP 0133 – Service Administration available methods in Jaxl 2.0

Jaxl 2.0 implements more than 10 XMPP extensions including XEP-0133 a.k.a. Service Administration. In this blog post, we will go through all the methods available for use in XMPP applications developed using Jaxl.

Using Service administration methods
You need to include Jaxl implementation of XEP-0133 in your application code to start using below listed available methods. Here is how this can be done at the top of your application code:

// initialize jaxl instance
$jaxl = new JAXL();

// include service administration
$jaxl->requires('JAXL0133'); // or jaxl_require('JAXL0133', $jaxl);

Service administration available methods
Below is a detailed list of methods from service administration extension:

  • $jaxl->JAXL0133(‘addUser’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘deleteUser’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘disableUser’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘reEnableUser’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘endUserSession’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘getUserPassword’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘changeUserPassword’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘getUserRoster’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘getUserLastLoginTime’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘getUserStatistics’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘editBlacklist’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘editWhitelist’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘getUserCount’, $user, $domain, $callback, $type)
  • $jaxl->JAXL0133(‘getUserList’, $user, $domain, $callback, $type)
  • $jaxl->JAXL0133(‘sendAnnouncementToActiveUsers’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘setMOTD’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘editMOTD’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘deleteMOTD’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘setWelcomeMessage’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘deleteWelcomeMessage’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘editAdminList’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘restartService’, $user, $domain, $callback)
  • $jaxl->JAXL0133(‘shutdownService’, $user, $domain, $callback)

All the methods expects 3 parameters as defined below:

  • $user: An associative array containing information about the user on whose account service administrative actions need to be taken
  • $domain: Jabber domain to which $user belongs
  • $callback: Specify where do you want to receive success/failure of executed service administration command
  • $type: Required by getUserCount and getUserList methods. Allowed values are registered, disabled, online, active and idle.

$user array MUST correspond to the fields in the request form for executing service administration commands.

Here is how to add a new user using XEP-0133:

$user = array(
        'jid' => 'newuser',
        'pass' => 'password',
        'email' => [email protected]',
        'fname' => 'Hello',
        'lname' => 'World'

$jaxl->JAXL0133('addUser', $user, 'jaxl.im', array($this, 'postAddUser'));

Your application code must have a methods named ‘postAddUser’ which will be called back by Jaxl core, when it finishes executing the command.

Jaxl 2.0 Core classes, available methods and directory structure

In this blog post we will dig deep into the core of Jaxl 2.0 – An XMPP framework written in PHP. Specifically, we will go through Jaxl core directory structure. Towards the end we will get familiar with various core classes and their available methods (e.g. $jaxl->sendMessage()), that developers can use in their XMPP applications.

Core Directory Structure
Now that you have the source code, lets get familiar with the Jaxl directory structure. Downloaded source code consists of following 5 directories:

  • xmpp: Contain core stack which implements XMPP rfc’s. All files follow naming convention like xmpp.*.php
  • core: Contains core stack which manages the library workflow. Also provides an xpath based XML parser, an event mechanism and various other utilities to the framework. All files follow naming convention like jaxl.*.php
  • xep: Contains implemented XMPP extensions (XEP’s). All files follow naming convention like jaxl.XEP-NUMBER.php
  • env: Contains Jaxl main() file jaxl.php, Jaxl core configuration file jaxl.conf and application environment setup file jaxl.ini. For bosh application developers jaxl.js is a must include from this directory.
  • app: Contains example applications which comes packed with Jaxl library

Core Classes and Available Methods
Below is a detailed list of all the core classes and available methods in developer land space:

This class is located inside core/jaxl.class.php and extends base XMPP class. Following is a list of methods available:

  • __construct($config=array()) : Constructor accepts following configuration parameters. Passed parameters will overwrite corresponding jaxl.ini values:
    $config = array(
        'user'=>'', // JAXL_USER_NAME
        'pass'=>'', // JAXL_USER_PASS
        'host'=>'', // JAXL_HOST_NAME
        'port'=>'', // JAXL_HOST_PORT
        'domain'=>'', // JAXL_HOST_DOMAIN
        'component'=>'', // JAXL_COMPONENT_HOST
        'logPath'=>'', // JAXL_LOG_PATH
        'logRotate'=>'', // false or second in which to rotate log
        'logLevel'=>'', // JAXL_LOG_LEVEL
        'pidPath'=>'', // JAXL_PID_PATH
        'boshHost'=>'', // JAXL_BOSH_HOST
        'boshPort'=>'', // JAXL_BOSH_PORT
        'boshSuffix'=>'', // JAXL_BOSH_SUFFIX
        'resource'=>'', // connecting user resource identifier
        'streamTimeout'=>'', // connecting stream timeout
        'sigh'=>'', // boolean to forcible enable/disable sigh term
        'dumpStat'=>'', // false or numeric specifying second after which to dump stat
  • auth($type) : Performs authentication with the jabber server. DIGEST-MD5, PLAIN, X-FACEBOOK-PLATFORM and ANONYMOUS are the supported auth $type
  • setStatus($status=FALSE, $show=FALSE, $priority=FALSE, $caps=FALSE) : Update the status of connected jabber user. Pass $caps=TRUE for sending entity capability information while setting connected user status.
  • subscribe($toJid)
  • subscribed($toJid)
  • unsubscribe($toJid)
  • unsubscribed($toJid)
  • getRosterList($callback=FALSE) : Fetch roster list for the connected user. See example usage inside echobot application.
  • addRoster($jid, $group, $name=FALSE)
  • updateRoster($jid, $group, $name=FALSE, $subscription=FALSE)
  • deleteRoster($jid)
  • sendMessage($to, $message, $from=FALSE, $type=’chat’)
  • sendMessages($to, $from, $child, $type)
  • sendPresence($to, $from, $child, $type)
  • $jaxl->requires($class) : It is an alternative for jaxl_require($class, $jaxl).
  • $jaxl->xml->xmlize($xml) : Use this method to convert raw XML string into an array.
  • $jaxl->log($log, $level) : Use this method for logging purposes

This class is located inside core/jaxl.plugin.php. Following is a list of methods available:

  • add($hook, $callback, $priority=10) : Register callback on available hooks
  • remove($hook, $callback) : Un-register callback on a previously registered hooks using add method

This class is located inside core/jaxl.cron.php. Use this call methods to run periodic background jobs. Following is a list of methods available:

  • add($callback, $interval) : $callback methods is called every $interval second
  • delete($callback, $interval) : Removes a previously registered cron job

This class is located inside core/jaxl.util.php. Following is a list of methods available:

  • isWin() : Window OS detection, return bool
  • getTime()
  • getBareJid($jid)
  • splitJid($jid)
  • curl($url, $type=’GET’, $headers=FALSE, $data=FALSE, $user=FALSE, $pass=FALSE)

More methods might get added in future and will be updated here.

Using available XEP methods
Once have included required XEP class inside your application code, either by using jaxl_require('JAXLxxxx', $jaxl) or by calling $jaxl->requires('JAXLxxxx'), you can start using the available methods inside included XEP class as follows:

$jaxl->JAXLxxxx('methodName', $param1, $param2, ....);

e.g. If you want to call joinRoom methods of Multi-User Chat XEP-0045, you can do so by calling

$jaxl->JAXL0045('joinRoom', $jid, $roomJid.'/'.$nick, $history, $seconds)

. DO-NOT call the method like JAXL0045::joinRoom(…)

Writing a command line XMPP bot (echobot) using Jaxl 2.0

In this blog post, we will write a sample XMPP bot (echobot) using Jaxl 2.0. In turn we will introduce ourselves to some of the basic functionality we can do using Jaxl e.g. fetching roster list, subscribing and unsubscribing to a user presence, etc. We will also focus on how to use XMPP extensions (XEP’s) inside our echobot code. Specifically, we will make use of XEP-0085 (chat state notification), XEP-0203 (delayed delivery) and XEP-0092 (software version) in our sample echobot application.

Echobot source code:
This sample echobot application comes packaged with Jaxl 2.0. If you have already read Installation, Usage guide and Running example apps, you might have even run this sample application.

Alternately, browse and download the sample source code from github echobot app files

Coding with Jaxl 2.0:
Every XMPP application developed using Jaxl 2.0, MUST have a jaxl.ini environment setup file inside your project directory. For example, packaged echobot sample application contains jaxl.ini which setup necessary Jaxl environment for our echobot application.

If you are developing an application from scratch, copy sample packaged jaxl.ini into your project folder:

cp /usr/share/php/jaxl/env/jaxl.ini /my/app/directory/jaxl.ini

and update JAXL_BASE_PATH and JAXL_APP_BASE_PATH with Jaxl installation path and your application directory path respectively.

Before we go on to write our echobot application code logic, lets see how an XMPP application code written using Jaxl 2.0 will generally look like:


        // Initialize Jaxl Library (#1)
        $jaxl = new JAXL();

        // Include required XEP's (#2)
                'JAXL0085', // Chat State Notification
                'JAXL0092', // Software Version
                'JAXL0203'  // Delayed Delivery
        ), $jaxl);

        // Sample Echobot class (#3)
        class echobot {
                function startStream() {}
                function doAuth($mechanism) {}
                function postAuth() {}
                function getMessage($payloads) {}
                function getPresence($payloads) {}
        $echobot = new echobot();

        // Add callbacks on various xmpp events (#4)
        JAXLPlugin::add('jaxl_post_connect', array($echobot, 'startStream'));
        JAXLPlugin::add('jaxl_get_auth_mech', array($echobot, 'doAuth'));
        JAXLPlugin::add('jaxl_post_auth', array($echobot, 'postAuth'));
        JAXLPlugin::add('jaxl_get_message', array($echobot, 'getMessage'));
        JAXLPlugin::add('jaxl_get_presence', array($echobot, 'getPresence'));


Lets break down the above applications code structure (note the #1, #2, #3, #4 markers in the above code)

  • #1 – Create a new Jaxl instance inside your application. By doing so Jaxl core functionality and other utilities are made available inside your application code. You can also pass parameters while initializing Jaxl instance
    $jaxl = new JAXL(array(
            'user'    =>    'username',
            'pass'    =>    'password',
            'host'    =>    'talk.google.com',
            'domain' =>    'gmail.com',
            'port'    =>    5222

    Passed parameters will overwrite the values specified inside jaxl.ini file

  • #2 – Include required XMPP extensions (XEP’s) inside your application code. jaxl_require is a special method which makes sure that no Jaxl core class loads twice inside a running Jaxl instance.

    To include an implemented XEP inside your application code simply use:

    jaxl_require('JAXL0203', $jaxl);

    where 0203 is the XEP number for Delayed Delivery extension, which will help our echobot know if an incoming message is an offline message. If you wish to include more than one XEP inside your application code, simply pass the list of XEP’s as an array:

    ), $jaxl);

    where 0085 is xep number for Chat state notification extension and 0092 is the xep number for Software Version extension.

    Since version 2.1.0, jaxl_require MANDOTORY accepts Jaxl instance which require core classes, in this case $jaxl. It helps Jaxl core to keep a track of required core classes for every Jaxl instances in a multiple-instance application.

  • #3 – Now that we have Jaxl core and required XEP’s in our application environment, it’s time to write our application code logic. Application code MUST have methods which are called back by the Jaxl core when specific xmpp events occur during your application run time. Optionally, Jaxl core can pass parameters to these methods during callbacks.
  • #4 – In the final step, we will register callbacks for necessary xmpp events using JAXLPlugin class add() method inside our application logic. Syntax:
    JAXLPlugin::add($hook, $callback);

    In this example, we want to receive callbacks in our application code for following available hooks and events:

    1. jaxl_post_connect: After Jaxl has opened stream socket to the jabber server
    2. jaxl_get_auth_mech: When Jaxl have information about supported auth mechanisms by the jabber server
    3. jaxl_post_auth: After Jaxl finish authenticating the bot with the jabber server
    4. jaxl_get_message: When a new message stanza is received by Jaxl
    5. jaxl_get_presence: When a new presence stanza is received by Jaxl

Writing application logic:
Till now we have the registered callbacks for various xmpp events, it’s time to catch these callbacks inside our echobot class and implement the required functionality of our application. Below is an explanation for some of the important pieces inside echobot class:

  • startStream() method is called when jaxl_post_connect event occurs. For our application we will simply send XMPP start stream by calling $jaxl->startStream()
  • doAuth($mechanism) method is called when jaxl_get_auth_mech event occurs in Jaxl core. Jaxl core passes list of supported auth mechanism by the jabber server. Proceed with a preferred auth type by calling $jaxl->auth() method
  • postAuth() is called after Jaxl library has finished authenticating our application bot with the jabber server
  • getMessage($payloads) gets all new messages as they are received by Jaxl core
  • getPresence($payloads) gets all new presence as they are received by Jaxl core

Running Echobot:
Enter your application directory (/usr/share/php/jaxl/echobot). It MUST contain jaxl.ini, open and edit connecting echobot username, password and jabber host. Then from command line run echobot as follows:

[email protected]:/usr/share/pear/jaxl/app/echobot# jaxl echobot.php
== 5617 == [[2010-08-03 10:37:21]] Socket opened to the jabber host jaxl.im:5222 ...
== 5617 == [[2010-08-03 10:37:22]] Performing Auth type: DIGEST-MD5
== 5617 == [[2010-08-03 10:37:28]] Auth completed...

Jaxl 2.0 – Installation, Usage guide and Example apps

This blog post provides detailed instructions on how to download and setup Jaxl 2.0 for quick XMPP application development using PHP. We will also see how to run XMPP bots using Jaxl command line utility (now available by just typing jaxl on the terminal).

Get the source code
Jaxl 2.0 development version source code is available at github.

  • For better experience download latest stable tarball from google code
  • The development version of Jaxl is hosted here at Github, have fun cloning the source code with Git. If you are not familar with Git just use the download button to get a tarball.
    [email protected]:~/git# git clone git://github.com/abhinavsingh/JAXL.git

Warning: the development source code is only intended for people that want to develop Jaxl or absolutely need the latest features still not available on the stable releases.

Installation on *nix Systems
Extract the downloaded tarball and enter source directory. The available build.sh file will help us installing Jaxl library at a preferred location on our system. Type ./build.sh help to view help instructions:

[email protected]:~/git# cd JAXL
[email protected]:~/git/JAXL# ./build.sh help

Build file will default install Jaxl library core under /usr/share/php/jaxl and Jaxl command line at /usr/bin/jaxl. Open build script, edit PACKAGE_INSTALL_PATH and PACKAGE_BIN_PATH to configure installation paths.

Issue following commands to setup Jaxl library core and Jaxl command line utility:

[email protected]:~/git/JAXL# mkdir /usr/share/php/jaxl
[email protected]:~/git/JAXL# ./build.sh
[email protected]:~/git/JAXL# ./build.sh install
uninstalling old package...
[email protected]:~/git/JAXL# touch /var/log/jaxl.log
[email protected]:~/git/JAXL# chown www-data /var/log/jaxl.log
[email protected]:~/git/JAXL# touch /var/run/jaxl.pid
[email protected]:~/git/JAXL# chown www-data /var/run/jaxl.pid

/var/log/jaxl.log is default log file for Jaxl applications and /var/run/jaxl.pid saves the process id (pid) of running Jaxl instances.

Usage guide
Now that we have setup Jaxl on our system, lets verify package installation by firing jaxl command line utility on the terminal:

[email protected]:~/git/JAXL# jaxl
Missing ini file...

If you get “Missing ini file” message, you have successfully verified package installation. This message is shown when current working directory doesn’t have jaxl.ini file, which is required by Jaxl cli utility before it can connects to a jabber server.

Running sample applications
Build script installs a sample application under /usr/share/php/jaxl/app/echobot directory. This directory contains two files namely:

  • echobot.php: Contains our echobot application code
  • jaxl.ini: It is application specific Jaxl configuration file

Open and update the following section inside jaxl.ini:

        // Connecting bot details
        define('JAXL_USER_NAME', 'username');
        define('JAXL_USER_PASS', 'password');

        // Connecting jabber server details
        define('JAXL_HOST_NAME', 'jabber.org');
        define('JAXL_HOST_PORT', 5222);
        define('JAXL_HOST_DOMAIN', 'jabber.org');

Now run the echobot from using jaxl cli utility:

[email protected]:/usr/share/php/jaxl/app/echobot# jaxl echobot.php
== 2946 == [[2010-08-02 14:37:53]] Socket opened to the jabber host jabber.org:5222 ...
== 2946 == [[2010-08-02 14:37:57]] Performing Auth type: DIGEST-MD5
== 2946 == [[2010-08-02 14:38:02]] Auth completed...

2946 is the process id of current running Jaxl instance. Same value can be found inside /var/run/jaxl.pid. Also tail the Jaxl log file for debug information:

[email protected]:~/git/JAXL# tail -f /var/log/jaxl.log
== 2946 == [[2010-08-02 14:38:02]] [[XMPPSend]] 123
chatOnline using Jaxl (Jabber XMPP Library in PHP)1

[[XMPPSend]] tells us that following XMPP packet was send by the Jaxl instance running with pid 2946. Also, total 123 bytes were transmitted over the socket.

Proceed to Writing your first command line bot using Jaxl for detailed explanation of the sample echobot application or if you are interested in building real-time web applications read Setup and Demo of Jaxl boshchat application