The XMPP-Protocol (which is the official name of the “Jabber”-Protocol) is released under open source, Jabber itself has many advantages in comparison to other instant messaging protocols:
- It’s open source
- It’s based on plain-text, xml-style data
- Everyone can set up a own Jabber-Server
- It’s decentralized: There are now “central” servers
There are some implementation of this protocol for PHP:
- XMPPHP (successor to class.jabber.php/CJB)
- PHP Jabber Client
All these classes are using different approaches – so have a short look. In this article I want to talk about XMPPHP, the PHP Jabber Client will be discussed in the next one.
XMPPHP
Projectpage/Download: Google Code
This class uses CJB as base, it’s easier to use than CJB itself. Similar to CJB it uses the send-wait-read-model; your script will send a message to the connected Jabber-server, wait for a reply and read the reply from the server. This model is easy to use, you can use linear programming, no event-handling is required – but this model can be slow down your script if you will use Jabber extensive.
XMPPHP supports joining chat-rooms and TLS encryption – without much effort. (The sample on the project page is wrong – the parameters are not in a valid order!)
First a simple code example:
<?php
include("xmpp.php");
//username = user without domain, "user" and not "user@server" - home is the resource
$conn = new XMPPHP_XMPP('my.server', 5222, 'username', 'password', 'home');
// Enables TLS - enabled by default, call before connect()!
$conn->useEncryption(true);
$conn->connect();
$conn->processUntil('session_start');
$conn->message('someguy@someserver.net', 'This is a test message!');
$conn->disconnect();
?>
This script will do:
- Connect to the talk.google.com-Jabber-Server
- Wait until the connection is successful established
- Send a message to someguy@someserver.net
- Close the connection to the Jabber-Server
Sending a message to a single account is easy – but this call won’t work with a chatroom. For this you have to do this:
...
$conn->connect();
$conn->processUntil('session_start');
// Enter the chatroom
$conn->presence(NULL, "available", "chatroom@server/NickName");
// Send message to chatroom - "groupchat" is required!
$conn->message("chatroom@server", "Test!, "groupchat");
// Leave the chatroom
$conn->presence(NULL, "unavailable", "chatroom@server/NickName");
$conn->disconnect();
...
Finally: How to read messages sent from other users to the used account or within a group-chat? This is event-based, you have to wait for the message-event:
...
$events = $conn->processUntil('message');
foreach($events as $current)
{
$data = $current[1]; // [0] contains the event type, here "message"
echo "Message - From: ".$data["from"].", Text: ".$data["body"];
}
...
You can also listen for more than one event (just use an array of strings, so you can wait for “message” and “presence” for example). When joining a chat-room you have to mention you will get also some “older” messages, not only new ones. Additional processUntil() accepts a timeout in seconds as second parameter.
Tobias is a guest writer – visit his blog http://www.mashempires.com, he will launch his start up KnowYourEfforts (a time tracking web application) within few months. Follow Elchie on http://www.twitter.com
October 20th, 2009 on 11:11 am
can the object be stored in the session and keep the connection to the xmpp server?
October 21st, 2009 on 2:43 am
Wow.
Thank you so much for this post. It was very well written and filled in a bunch of gaps I was unable to find in the code or in any examples.
Getting the eJabber cluster up turned out to be the easy part, in that I've spent 3x the time trying to get a message from PHP into a chatroom.
You, good sir, are a gentleman and a scholar.
Thanks again
Jop
November 5th, 2009 on 5:57 pm
Can you please let me know, how can i keep the connection alive because once the script ceases to run,the connection is broken. Or can u plz tell me of a way to make the timeout infinite???
Thnx
Abhishek