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 (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
<handshake>4d6c2e762d5ba5dca2cbd3a90a4deeb6a6fa0838</handshake>

[15008] 2010-08-24 01:40:05 - [[XMPPGet]]
<handshake/>

[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!

  • Pingback: Webby Scripts How to write External Jabber Components in PHP using Jaxl library …()

  • 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?

  • 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.

  • Hawkwins

    Hi,

    Sorry for posting it again, I forgot where I post the last message.

    My question is whether can i use jaxl php library without installing it?

    Thanks in advance

  • esmin

    I have jabber server. What is simplest method to send a message (to some user(s)) via php without cli, but from browser php call

  • Abhi is there a benchmark for traffic that jaxl component bots can handle? Is there any support planned for libevent (i see a post from u related to php+libevent).

  • 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.

  • such as,A send a message ‘hello B’ to B,xmpp server send the message to B and forward the message to a bot.how can i implement ‘forward’?thx.

  • 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 ?

    Thanks

    • 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.

  • 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?

    Thanks!
    Deedee

    • Indeed Jaxl library allows you to do that…. see sample examples under “/apps” folder

  • 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?

    Thanks…

  • Thank you for the library and it’s documentation throughout your blog. Very awesome effort.

    Cheers.

  • Announcing Jaxl v3.x – asynchronous, non-blocking I/O, event based PHP client/server library – http://abhinavsingh.com/blog/2012/07/announcing-jaxl-v3-x-asynchronous-non-blocking-io-event-based-php-clientserver-library/