JAXL – Open Source Jabber XMPP Library

Posted on

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”
<?php
// Set an enviornment
$env = “prod”;
// Log Level for logger class
$logEnable = TRUE;
// Log in MySQL database
$logDB = FALSE;
$key = array(“prod”=>array(“user”=>“myproductionuser”,
“pass”=>“password”,
“host”=>“talk.google.com”,
“port”=>5222,
“domain”=>“gmail.com”
),
“devel”=>array(“user”=>“mydevelopmentuser”,
“pass”=>“password”,
“host”=>“localhost”,
“port”=>5222,
“domain”=>“localhost”
)
);
$db = array(“prod”=>array(“dbuser”=>“root”,
“dbpass”=>“password”,
“dbhost”=>“localhost”,
“dbname”=>“jaxl”
),
“devel”=>array(“dbuser”=>“root”,
“dbpass”=>“password”,
“dbhost”=>“localhost”,
“dbname”=>“jaxl”
)
);
?>

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.
<?php
function eventMessage($fromJid, $content, $offline = FALSE) {
if($offline) {
$this->sendMessage($fromJid,“Hi, Thanks for your offline message”);
}
else {
$this->sendMessage($fromJid,“Hi, Thanks for your message”);
}
if($this->logDB) {
// Save the message in the database
$timestamp = date(‘Y-m-d H:i:s’);
$query = “INSERT INTO message (FromJid,Message,Timestamp) value (‘$fromJid’,’$content’,’$timestamp’)”;
$this->mysql->setData($query);
}
}
function eventPresence($fromJid, $status, $photo) {
// Change your status message to your friend’s status
$this->sendStatus($status);
if($this->logDB) {
// Save the presence in the database
$timestamp = date(‘Y-m-d H:i:s’);
$query = “INSERT INTO presence (FromJid,Status,Timestamp) value (‘$fromJid’,’$status’,’$timestamp’)”;
$this->mysql->setData($query);
}
}
?>

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)
<?php
/* Include Key file */
include_once(“config.ini.php”);
/* Include JAXL Class */
include_once(“jaxl.class.php”);
/* Create an instance of XMPP Class */
$jaxl = new JAXL($key[$env][‘host’], // Jabber Server Hostname
$key[$env][‘port’], // Jabber Server Port
$key[$env][‘user’], // Jabber User
$key[$env][‘pass’], // Jabber Password
$key[$env][‘domain’], // Jabber Domain
$db[$env][‘dbhost’], // MySQL DB Host
$db[$env][‘dbname’], // MySQL DB Name
$db[$env][‘dbuser’], // MySQL DB User
$db[$env][‘dbpass’], // MySQL DB Pass
$logEnable, // Enable Logging
$logDB // Enable MySQL Inserts
);
try {
/* Initiate the connection */
$jaxl->connect();
/* Communicate with Jabber Server */
while($jaxl->isConnected) {
$jaxl->getXML();
}
}
catch(Exception $e) {
die($e->getMessage());
}
?>

For Complete Documentation go through below link:

Leave a Reply

Your email address will not be published. Required fields are marked *