Wednesday, March 9, 2011

Facebook chat using PHP

Start using Facebook chat in your web sites. It is easy, interesting and gives new opportunities. Read more. 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” server
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:

This script will do:
  1. Connect to the talk.google.com-Jabber-Server
  2. Wait until the connection is successful established
  3. Send a message to someguy@someserver.net
  4. 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:

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:

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.

...
$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"];
}
...

...
$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();
...

<?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();
?>

No comments:

Post a Comment