JAXL – Open Source Jabber XMPP Library

March 16th, 2012 by laeeq | 2 comments

JAXL stands for “Jabber XMPP Library“.

Jaxl 2.x is an object oriented XMPP framework in PHP for developing real time applications for browsers, desktops, facebook chat,gtalk and hand held devices. Jaxl 2.x is a robust, flexible and easy to use version of Jaxl 1.x series which was hosted at google code.

This library currently supports following features:

  • Connect to a Jabber Server (e.g. Gtalk)
  • TLS Encryption
  • DIGEST-MD5 and PLAIN authentication mechanisms
  • Roster Support

Library comes with the following class files:

  • config.ini.php : Holds your jabber account and mysql connection information
  • mysql.class.php : Basic MySQL connection class used to insert received messages and presence into MySQL database
  • logger.class.php : A very basic logger class which allows you to log all XML stanza’s send and received from jabber server
  • xmpp.class.php : Base XMPP class library which implements the XMPP protocol
  • jaxl.class.php : JAXL class which extends XMPP class library. Should be the starting point for your application.
  • index.php : After building your application in jaxl.class.php, you finally initializa and call your methods here

What’s new with Jaxl 2.x?

  • More robust, flexible, scalable and easy to use
  • Event mechanism for registering callbacks for various xmpp events
  • Integrated support for Real Time Web (XMPP over Bosh) application development
  • 32 implemented XMPP extensions (XEP’s)
  • Setup dynamic number of parallel XMPP sessions on the fly
  • Options for monitoring, usage stat collection, rate limiting, etc.

Download

How to use JAXL:

JAXL Client Library is highly structured. There is a base XMPP class library (xmpp.class.php) and a JAXL class library (jaxl.class.php) which is derived from the base XMPP class library.
Base XMPP class library implements the XMPP protocol and it also provides you with two extendable methods named eventMessage() and eventPresence(). These methods are internally called when a message or presence XML Stanza is received from the Jabber server.
Jaxl.class.php must be the starting point for all your applications. Simply customize the eventMessage() and eventPresence() Method for your application. Other methods which might interest you while customizing them are:

  • sendMessage($jid,$message) : For sending message to a particular Jid
  • sendStatus() : To set your status message
  • roster(‘get’) : To get list of roster
  • roster(‘add’,$jid) : Add a new contact in roster list
  • roster(‘remove’,$jid) : Remove a contact from roster list
  • roster(‘update’,$jid,$name,$groups) : Update a particular contact in roster
  • subscribe($jid) : Subscribe for presence of a particular $jid

This library includes the following files:

Config File (config.ini.php)

You specify all your jabber account username and password in this file. For development environment keep $env = “devel” and for production simply change it to $env = “prod”

  1. // Set an enviornment
  2. $env = “prod”;
  3. // Log Level for logger class
  4. $logEnable = TRUE;
  5. // Log in MySQL database
  6. $logDB = FALSE;
  7. $key = array(“prod”=>array(“user”=>“myproductionuser”,
  8. “pass”=>“password”,
  9. “host”=>“talk.google.com”,
  10. “port”=>5222,
  11. “domain”=>“gmail.com”
  12. ),
  13. “devel”=>array(“user”=>“mydevelopmentuser”,
  14. “pass”=>“password”,
  15. “host”=>“localhost”,
  16. “port”=>5222,
  17. “domain”=>“localhost”
  18. )
  19. );
  20. $db = array(“prod”=>array(“dbuser”=>“root”,
  21. “dbpass”=>“password”,
  22. “dbhost”=>“localhost”,
  23. “dbname”=>“jaxl”
  24. ),
  25. “devel”=>array(“dbuser”=>“root”,
  26. “dbpass”=>“password”,
  27. “dbhost”=>“localhost”,
  28. “dbname”=>“jaxl”
  29. )
  30. );

MySQL Class (mysql.class.php)
This is a basic MySQL connection class used to insert received messages and presence into MySQL database

Base XMPP Class (xmpp.class.php)
You should not worry about this class. Until and unless you are aware of what are you trying to achieve you should not touch this class file.

Logger Class (logger.class.php)
This is a basic logger class. It help you log XML send to and received from jabber server. Might prove helpful in debugging and if you are interested in learning the core of XMPP Protocol.

Extended JAXL Class (jaxl.class.php)
You will be customizing eventMessage() and eventPresence() methods here.

  1. function eventMessage($fromJid, $content, $offline = FALSE) {
  2. if($offline) {
  3. $this->sendMessage($fromJid,“Hi, Thanks for your offline message”);
  4. }
  5. else {
  6. $this->sendMessage($fromJid,“Hi, Thanks for your message”);
  7. }
  8. if($this->logDB) {
  9. // Save the message in the database
  10. $timestamp = date(‘Y-m-d H:i:s’);
  11. $query = “INSERT INTO message (FromJid,Message,Timestamp) value (‘$fromJid’,'$content’,'$timestamp’)”;
  12. $this->mysql->setData($query);
  13. }
  14. }
  15. function eventPresence($fromJid, $status, $photo) {
  16. // Change your status message to your friend’s status
  17. $this->sendStatus($status);
  18. if($this->logDB) {
  19. // Save the presence in the database
  20. $timestamp = date(‘Y-m-d H:i:s’);
  21. $query = “INSERT INTO presence (FromJid,Status,Timestamp) value (‘$fromJid’,'$status’,'$timestamp’)”;
  22. $this->mysql->setData($query);
  23. }
  24. }

In above example there are 4 things:

  • Sends back a message saying “Hi, Thanks for your offline message”, when I receive a offliner.
  • Sends back a message saying “Hi, Thanks for your message”, when I receive an IM from my friend.
  • Change my status message, as and when I receive a status update from my friends.
  • Save messages and presence into database if $logDB = TRUE in config.ini.php

Final Call (index.php)

  1. /* Include Key file */
  2. include_once(“config.ini.php”);
  3. /* Include JAXL Class */
  4. include_once(“jaxl.class.php”);
  5. /* Create an instance of XMPP Class */
  6. $jaxl = new JAXL($key[$env]['host'], // Jabber Server Hostname
  7. $key[$env]['port'], // Jabber Server Port
  8. $key[$env]['user'], // Jabber User
  9. $key[$env]['pass'], // Jabber Password
  10. $key[$env]['domain'], // Jabber Domain
  11. $db[$env]['dbhost'], // MySQL DB Host
  12. $db[$env]['dbname'], // MySQL DB Name
  13. $db[$env]['dbuser'], // MySQL DB User
  14. $db[$env]['dbpass'], // MySQL DB Pass
  15. $logEnable,            // Enable Logging
  16. $logDB                 // Enable MySQL Inserts
  17. );
  18. try {
  19. /* Initiate the connection */
  20. $jaxl->connect();
  21. /* Communicate with Jabber Server */
  22. while($jaxl->isConnected) {
  23. $jaxl->getXML();
  24. }
  25. }
  26. catch(Exception $e) {
  27. die($e->getMessage());
  28. }

For Complete Documentation go through below link:

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. April 19th, 2012 at 06:14 | #1

    I have to express aoareciptipn to this writer just for rescuing me from this type of setting. As a result of looking out throughout the the web and seeing basics that were not helpful, I assumed my entire life was gone. Existing without the solutions to the problems you’ve sorted out by way of your good guideline is a crucial case, and those which might have negatively affected my entire career if I had not encountered your web blog. The know-how and kindness in controlling all the details was important. I am not sure what I would’ve done if I hadn’t encountered such a solution like this. I am able to at this time relish my future. Thanks for your time very much for the professional and effective guide. I won’t think twice to endorse your web blog to anybody who will need assistance on this problem.

  2. braydenreynoldspq
    October 3rd, 2012 at 07:14 | #2

    hi!,I love your writing very a lot! percentage we communicate extra approximately your article on AOL? I need an expert on this space to unravel my problem. May be that’s you! Looking forward to look you.

  1. No trackbacks yet.