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

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

All incoming stanzas addressed to that domain (to='') or to entities on that domain (to='[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!


  1. Pingback: Webby Scripts How to write External Jabber Components in PHP using Jaxl library …

  2. rutwick

    Hello Sir,
    I used your example to get the contacts list. But it tells me that rosterlist is undefined. Please can you help me with this error?
    Another question I wanted to ask… Is there a way I can set a custom status message on gtalk/gchat using PHP?

  3. Hi rutwick,

    What exactly are you trying to achieve? Are you trying to fetch roster list of connected component bot? or is it for a connected client bot?

    Yes, you can set custom status message on gtalk, facebook chat etc using Jaxl library.

  4. hi Abhi,your great work help me so much,thanks.but i still have another question: i want to my xmpp server forwards all message it received to a bot on my web server.please give me some advices,thx.

  5. Hello, I’ve a community website where I want to add a chat between users in the community AND user > facebook friends.
    So, for instance, when a user login to my community AND is logged to FB see a list with BOTH facebook’s friends and community’s friends.
    Is it possible to have this sort of “double” connection (one with my local XMPP server and one with FB server) with Jaxl ?
    Is this the way to handle this scenario ? Or is better to setup my XMPP server to act as a “proxy” to FB XMPP server ?


    1. Indeed you can have similar multiple conn. but this has to be handled mostly on the server side. Indeed you would like to have a BOSH server of your own which can relay stanza’s back on the user browser.

  6. Deedee

    Found some free time, and was wondering in which language i should write an app that uses jabber protocol.

    I want this app to ‘get’ an online contact list from the logged on jabber user.

    Is this possible?


  7. Jesse Norell

    I just restarted my ejabberd, and my jaxl component bot “thinks” it’s still connected, but I can’t send messages to it since the server no longer has that route – I have to stop / restart my jaxl bot to get going again. I figure maybe someone has worked through these type of issues already; do you know of a good example of a production-ready component bot?


Comments are closed.