Introducing JAXL – Open Source Jabber XMPP Library
Introduction
JAXL stands for “Jabber XMPP Library“. For fun, it stands for “Just Another XMPP Library”
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
Source Code
JAXL is hosted at Google Code. Checkout full source code from here:
http://code.google.com/p/jaxl
|
|
| jaxl |
| Visit this group |
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”
// 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.
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 I have done 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)
/* 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());
}
Instant Messenger Powered by JAXL
I am pleased to announce 3 months after release of JAXL, 1st Instant Messenger powered by JAXL. Currently the instant messenger is in testing phase and will be released after some 3-4 weeks of thorough testing. Meanwhile here is the first clip shot:
Disclaimer
Currently this library is being developed and used at Gtalkbots.com . Though it is thoroughly tested and being used, I still recommend not to use it on production servers. Mainly, since it is heavily customized for Gtalkbot’s usage. I am releasing it, as I see it of use for the community worldwide. If you want to use it, use at your own risk. Let me know of any changes you make to this library. In future I look to make this library more generic.
Enjoy and let me know of any bugs, feedbacks, enhancements or any abuse you may want to pass through if it doesn’t work for you
You should be running index.php using command line and not browser (recommended). This class is currently highly customized for Gtalkbots usage and works perfectly with Gtalk Servers.
-
3 Trackback(s)
- Jan 2, 2009: Behind the scenes - How and What XML’s are exchanged by JAXL | Abhi's Weblog
- Feb 16, 2009: Programatically control your google mails using JAXL v 1.0.4 | Abhi's Weblog
- May 20, 2009: How to broadcast a message to your Gtalk friends using JAXL? | Abhi's Weblog























































45 Comment(s)
By x1311 on Jan 4, 2009 | Reply
Hi there,
very interesting Library. What I am mostly interested in is how eventMessage() gets called internally. Could you describe the whole message flow with core components?
By admin on Jan 4, 2009 | Reply
Yeah in the case study of google talk server http://abhinavsingh.com/blog/2009/01/behind-the-scenes-how-and-what-xmls-are-exchanged-by-jaxl/, i did missed the XML stanza’s being transmitted when a message is sent or received.
Here is what JAXL receives when a message is sent:
<message to=”myproductionuser@gmail.com” type=”chat” id=”325″ from=”friend_2@gmail.com/Talk.v104233E14DB”><body>Hi dood</body><active xmlns=”http://jabber.org/protocol/chatstates”/><nos:x value=”disabled” xmlns:nos=”google:nosave”/><arc:record otr=”false” xmlns:arc=”http://jabber.org/protocol/archive”/></message>
So you can see that along with incoming XML, Gtalk server also tells you about the features supported by Gtalk server.
Here is what you will receive for an offline message:
<message to=”myproductionuser@gmail.com” from=”friend_2@gmail.comm”><body>Hi dood</body><x stamp=”20090104T16:27:25″ xmlns=”jabber:x:delay”/><time ms=”1231086445192″ xmlns=”google:timestamp”/></message>
Hence from the incoming XML stream, you know that it’s an off liner.
I hope this is what you asked for. See the example jaxl.class.php where I have handled both online and offline messages separately.
Enjoy.
By x1311 on Jan 4, 2009 | Reply
Ok, thank you. I´ll take a deeper look.
By x1311 on Jan 4, 2009 | Reply
I was just wondering how you could receive the incoming messages. I did not code php for a long time. But I just found the streams in the documentation
By admin on Jan 4, 2009 | Reply
Yeah when you connect to gtalk server JAXL will log each incoming and outgoing streams in logger.log file under /log directory. Simply set $logEnable = TRUE in config.ini.php.
By namespace on Jan 4, 2009 | Reply
Hey, First of all thanks for such a great library. There are many available in PHP, but JAXL is one of the best documented library for me.
By Vinayak Joshi on Jan 14, 2009 | Reply
Hi Abhi
Many thanks for sharing your excellent work with all. We are newbies trying to code a chatbot and your library is very helpful. I have two off-topic comments to make:
1. The wordpress theme and especially its name is great.
2. How are these geometric image avatars generated for commenters on your blog?
Regards
Vinayak
By admin on Jan 14, 2009 | Reply
Thanks Vinayak. I would love to list your application on Jaxl’s google code page. Kindly let me know when you are done with your application.
About the theme name, well that was my nick name back in college, so tried playing with that.
About the Geometric Image Avatars, well they are generated by Gravatar. Nothing to do with my theme or code.
Do lemme know about your application and all the best
By Gaurav on Jan 14, 2009 | Reply
Hi
I am trying to learn how to create gtalk powered bots. I want to create a bot that translates words from english to hindi. For example, if you send the word “One” the bot should reply back with “ek”. For this purpose I have created a php script that takes a word as input, looks up in a database and returns an output.
Now I want to connect this php script to the bot. I have also created an account on gmail to act as a bot [dictionaribot@gmail.com].
My question is that now how do I connect this php script to the gmail bot? What is the role of Jaxl in this? My understanding of the process is this:
1. User sends keyword through gtalk to gmail jabber server
2. Gmail jabber server sends the message to dictionaribot@gmail.com
3. dictionaribot… transmits the keyword to the php script
4. the php script looks up keyword in mysql database and returns a value
5. the value is transmitted to the user through the jabber server.
My confusion is as to how is information transferred in steps 3 and 5 above. how is the connection between the gtalk message and the php script is made?
I am sorry if the question is stupid but I am a beginner and trying to just enter into this field.
Thanks,
Gaurav
By admin on Jan 14, 2009 | Reply
Hi Gaurav,
No worries, we all start from basic. What you want to achieve is quite straight forward if you are using JAXL. I hope by now u understand how to make use of JAXL.
For your application you simply need to customize the eventMessage() method in jaxl.class.php.
From my guess it will look like this:
<code>
function eventMessage() {
if(!$offline) {
// Grab the sentence passed by the user
$sentence = trim($content);
// Now simply lookup in your database or use google language api’s to convert this sentence to hindi
$translatedSentence = database_lookup($content);
// If you wish acknowledge JAXL library
$translatedSentence .= “\npowered by Jaxl http://code.google.com/p/jaxl\n”;
// Finally send back the translated sentence
$this->sendMessage($fromJid,$translatedSentence);
}
}
</code>
You simply have to replace the database_lookup function with your application code. (For more idea see the source code of jaxl4dzone.class.php)
However I guess there is already some bots released by Google themselves for such translations.
All the best! Do lemme know when you are done with your application, I will list that on the Jaxl google code page
By Gaurav on Jan 14, 2009 | Reply
Hi Abhinav
Thanks for the prompt reply. I would request you to be a little patient with me on this.
I have understood the way to handle the operations once the message is recd by the php file.
However, I am confused as to how to send messages received on the dictionaribot@gmail.com id to the index.php file on my web server.
Right now this is what is happening:
user@gmail.com > dictionaribot@gmail.com
But for the bot to work, what should happen is:
user@gmail.com > dictionaribot@gmail.com>index.php>mySQL>index.php>dicitonaribot@gmail.com > user@gmail.com
How does one transmit info from the jabber id to the php file?
Thanks in advance for your support.
By Gaurav on Jan 14, 2009 | Reply
To clarify the above message – how do I initiate the execution of the index.php page? It is lying on my XAMPP web server. Should I create a cron job that executes index.php after a set period of time.
By admin on Jan 14, 2009 | Reply
I guess the following workflow will help you:
Simply set $logDb = TRUE in config.ini.php and then change the $query inside jaxl.class.php to what ever query you want to. Get back the required translation from the database and send it back to the user.
I hope this shd definitely get you going. All the best.
PS: No need of any cron job or any other trick.
By Gaurav on Jan 14, 2009 | Reply
Sorry to bother you again. But the place where I am still confused is quoted below:
<quote>
send “One” to this class from within eventMessage() method and receive “ek”
</quote>
How this will happen both are separate containers?
Thanks in advance
By admin on Jan 14, 2009 | Reply
I guess u need to just read a little about OOPS. You can very well make a class out of your application which queries the database and gets the translated data. Then include this class in jaxl.class.php, create a new object. Pass $content using this object to your application class and get back the translated results in the jaxl.class.php itself.
I guess give a lill fight and u will get thr. All the best, leaving for party
By Vinayak on Jan 15, 2009 | Reply
Hi
I have been reading with interest the discussion between Abhi and Gaurav. Abhi – I think the continued confusion is because the problem is not clear and you are offering the solution for the wrong problem. I made a diagram showing the process and the part where gaurav is facing a problem.
Take a look: http://i41.tinypic.com/24vl6×5.jpg
The problem I think lies in part 2 of the process. While Abhi is telling the solution for part 3 and 4, which I think Gaurav has worked out using Jaxl.
Gaurav, pls confirm my understanding.
By admin on Jan 15, 2009 | Reply
Well I m not sure how could I hve made a similar diagram, but here is the flow diagram which I would like to present.
PS: I have uploaded this image on tinyurl so that it doesn’t vanish for future readers
By Gaurav on Jan 15, 2009 | Reply
Yes I agree with Vinayak. Now there is only 1 doubt left i.e.
how to get the bot running on the server?
More specifically, please refer to this line of your explanation:
» dictionaribot@gmail.com is running as a bot on gaurav’s php server
I tried executing the index.php file and got this warning:
Warning: fwrite(): supplied argument is not a valid stream resource in /home/iilvin/public_html/dictionari/jaxl/xmpp.class.php on line 152
I am also copy pasting Log file entries:
Log file entries:
2009-01-15 03:25:02
Initializing class variables
2009-01-15 03:25:02
Trying to connect at talk.google.com:5222
2009-01-15 03:25:23
Failed to establish a connection at talk.google.com:5222
2009-01-15 03:25:23
Sending XML>>
</stream:stream>
Thanks
By admin on Jan 15, 2009 | Reply
“dictionaribot@gmail.com is running as a bot on gaurav’s php server“
By Gaurav on Jan 15, 2009 | Reply
Thanks Abhi
I’ll contact the host provider.
By admin on Jan 18, 2009 | Reply
For all further discussions I would encourage you to join the JAXL google group, and take part in on-going discussions.
By admin on Jan 29, 2009 | Reply
Hi Gaurav,
By Gaurav on Jan 29, 2009 | Reply
Hi Abhi,
The delay has been because the hosting provider had an elaborate mechanism for granting permission for shell access. We got the permission to run commans through commandline yesterday only.
Now when I login I get the following response:
log: Initializing class variables
log: Trying to connect at talk.google.com:5222
log: Failed to establish a connection at talk.google.com:5222
log: Sending XML>>
</stream:stream>
Warning: fwrite(): supplied argument is not a valid stream resource in /home/iilvin/public_html/dictionari/jaxl/xmpp.class.php on line 152
It seems we need to specifically ask permission for running the commands that you mentioned. I am raising a ticket for the same today. Will keep you informed of the progress.
By Gaurav on Feb 3, 2009 | Reply
Hi Abhi,
I got this reply from my hosting provider:
Commands allowed: “fsockopen”, “stream_set_blocking”
Commands not allowed: “stream_set_timeout”
Pls suggest me if there is any workaround to the command – stream_set_timeout.
Or if you could suggest a web host that supports all the required configurations. Where are you hosting your site?
Regards
By admin on Feb 3, 2009 | Reply
well i doubt you can go ahead with this hosting then. Well I have my own virtual private server so I have full control on my server. You may want to try x10hosting.com. I used it when I started web development, though I am not sure if you have these commands allowed there again.
By Gaurav on Feb 4, 2009 | Reply
Hi Abhi,
I am planning to go for my own virtual private server. Vendors are asking for server configration. Pls suggest if the configration below is fine or any changes are required?
Disk Space: 10 GB
Monthly Bandwidth Transfer: 100 GB
cPanel Version: 10.x or above
Apache Version: 2.2.x (Unix)
PHP Version: 5.2.8
MySQL version: 5.x or above
Architecture: x86_64
Operating System: Linux [which flavor would you suggest?]
RAM: 2GB
By admin on Feb 4, 2009 | Reply
Gaurav it depends on your need and how much resources you will be needing. I guess you can go ahead with the above configuration of disk space and ram. But I didn’t get why did you mentioned the PHP, Apache and MySQL versions. That is all in your hand and you will need to configure yourself, unless your VPS hosting provider is giving you pre-configured VPS….
By yuva on Feb 4, 2009 | Reply
i want anagrams games bot…
can i get the coding.. for that…
By yuva on Feb 4, 2009 | Reply
Anyhow i am working with jaxl library.. to create an anagram games bot like gtalkbots.com … I am not a hardcore programmer So i dont know the % of success … if that gtalkbots anagram code is an open source then i need hardly need that code… Thank You for Developing Such an good jaxl Library..
By admin on Feb 4, 2009 | Reply
Thanks Yuva….Well the plan if ofcourse to release that too. I have already released a few sample applications and target is to keep producing more sample applications which can show the power of JAXL and XMPP Protocol.
By yuva on Feb 4, 2009 | Reply
I have Contacted You By Mail
By Gaurav on Feb 11, 2009 | Reply
Hi Abhi
I now have access to a VPS with LAMP. I was able to get the dictionaribot working finally using JAXL.
Next I installed a jabber server (jabberd) and tried to create an account using the Exodus chat client installed on my local windows machine. However I got this error:
<code>
Looking up SRV: _xmpp-client._tcp.iilv.in
Got A: 67.223.233.252 5222
SENT: <stream:stream to=”iilv.in” xmlns=”jabber:client” xmlns:stream=”http://etherx.jabber.org/streams” xml:lang=”en” version=”1.0″ >
RECV: <?xml version=’1.0′?><stream:stream xmlns:stream=’http://etherx.jabber.org/streams’ id=’49919FC7′ xmlns=’jabber:client’ from=’iilv.in’>
SENT: <iq id=”jcl_2″ type=”get”><query xmlns=”jabber:iq:auth”><username>abcd</username></query></iq>
RECV: <stream:error>Disconnected</stream:error></stream:stream>
</code>
Can you help me with this?
Many thanks in advance.
By admin on Feb 11, 2009 | Reply
Hi Gaurav,
A quick google search gives me this, probably this can be of some help:
http://www.google.co.in/search?hl=en&rlz=1C1GGLS_enIN291IN304&q=Looking+up+SRV:+_xmpp-client.&btnG=Search&meta=
By Gaurav on Feb 16, 2009 | Reply
Hi Abhi
The bot is not functional as yet. We are still struggling with a few issues. I will let you know all the details once we get it going. Many thanks for your ongoing help and interest.
There are 2 main issues that we face:
1. We are running the php file by logging on to our VPS through the console provided by PuTTY which is a free telnet and SSH Client for Windows and Unix platforms. But as soon as I close the PuTTY executable on my local machine, the bot goes offline on the VPS.
2. Can JAXL work with non-gmail bots? Can I host my own bot?
Thanks
Gaurav
By Daniel Alonso on Mar 30, 2009 | Reply
Hello, i need to add a jabber web client to my wordpress web page but my emplyer dont want to use the wordpress plugin. I`m trying to use the JAXL library but when i set up the config file, i get this error
<failure xmlns=’urn:ietf:params:xml:ns:xmpp-sasl’>
<not-authorized/>
</failure>
I add a new test user to my jabber server (I`m using EjabberD) and double check the username and password.
Any idea?
Thanks in advance
By admin on Mar 30, 2009 | Reply
Hi,
Strange enough. BTW can you confirm if you have updated your username and password under the right enviornment, i.e. probably you have added ur username and password under dev enviornment and you have $env = “prod” set.
Also can you kindly send in the complete logs till you get the above error for more debugging.
By Vinayak on Apr 16, 2009 | Reply
Hi Abhi
In your anagram bot, how do you remember the user’s state? For example if you have sent him a jumbled word and he asks for a hint, how do you know which word’s hint should be sent. Because at any time, a number of users must be chatting with the bot. And each user could be on a different word.
Do you create some session or something? Or do you store and retrieve values in a db?
Thanks in advance
By admin on Apr 16, 2009 | Reply
You got it wrong actually. At any point in time, all users are trying to answer the same jumbled word. The one who answers first (and believe me the difference is in micro seconds) wins the round.
By Pankaj on May 26, 2009 | Reply
Hi, I have a debian server. I was using jaxl library but now i am using xmpphp library for bot development as i have to use bosh .
Is there any provision of bosh under jaxl library?
When i create a bot using xmpphp library i.e through xmpp file its working fine but when i try to use bosh it generates a fatal error.
Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /var/www/testlib/XMPPHP/BOSH.php:75 Stack trace:
0 /var/www/testlib/XMPPHP/BOSH.php(75): SimpleXMLElement->__construct(”)
1 /var/www/testlib/index.php(14): XMPPHP_BOSH->connect(’myserver’)
2 {main} thrown in /var/www/testlib/XMPPHP/BOSH.php on line 75
Can you Please help me on this?
Thanks
Pankaj
By admin on May 26, 2009 | Reply
By Pankaj on May 26, 2009 | Reply
Hi,
Thanks for your prompt reply.
My main reason of moving out to xmpphp was bosh support specifically for state remembrance/session maintenance .
Regards,
Pankaj
By admin on May 26, 2009 | Reply
Yup, indeed. You may try your luck with xmpphp till JAXL releases bosh library extension.