Moods, Tutorials

My chat widget with PHP

2 Comments 04 October 2010

My chat widget with PHP

Some of you may have noticed the new widget placed in the sidebar of this blog. I wanted to display in real-time the number of people registered in my chat room, and indicate if I’m in logged in or not. First, I tried using the UserManager AS3 class available in the LCCS Flex SDK. It provides a direct access to the list of users, but to get this list, you need to authenticate (and then it would add a fake user at every connection). That’s why I decided to look at the server side API.

If you open the LCCS SDK, you ‘ll find a access to the “Server libraries“. These libraries allow you to connect to LCCS and manage accounts (for server-side authentications), create rooms on the fly or from templates, and publish or subscribe messages in real-time. You can play with several languages to access LCCS: Java, Ruby, Python, ColdFusion, Groovy, C# and PHP! As my blog is based on WordPress, which is based on PHP… I made my choice quite easily.

The APIs fall into three groups. The Calls, the Queries and the Hooks. Thanks to the Calls, you can perform operations on the service. Calls are asynchronous. You can, for instance, set a user role, create a new node, publish a new item… The Queries are for fetching information from the service. You can get information about your Hooks, about the configuration of your nodes, or information about the items inside your application nodes. Hooks are very interesting as they are notifications of events occurring on the service. It uses an AMF gateway to perform notifications and add an event listener.

To get the list of the users for my widget, I just need to authenticate using my LCCS developer credentials, and use the fetchItems method (a Query). The fetchItems method needs a room, a collection name and a node name. In this case, I’ll pass the “UserManager” collection, and the “UserList” node. It returns an XML-RPC document. To parse it, I used the DomDocument PHP API… and I know it’s not the best one to use. I tried to find a good library to automatically consume XML-RPC formatted responses without success. I know that Mihai, who is our Flex-PHP expert at Adobe, should be able to give me some good advice.

Once I’ve got the XML response, I loop on the ‘item’ tag to get the number of users. I also check if the owner of the chat (me) is logged-in. The owner has a 100.00 role level.

Here is the code of the widget. The lccs.php is the library you need to include to use the LCCS PHP APIs.

$account = new RTCaccount("https://collaboration.adobelivecycle.com/michaelchaize");
	$passwd = "password";
	$account->login("username@adobe.com",$passwd);
 
	$messages = $account->fetchItems("webchat","UserManager","UserList");
 
	$dom = new DomDocument();
	$dom->loadXML($messages);
	$racine = $dom->documentElement;
 
  $listeUsers = $dom->getElementsByTagName('item');
 
	$nbUsers = 0;
	$michaelPresence = "OFFLINE";
  foreach($listeUsers as $useritem)
  {
	 $nbUsers += 1;
	 $listeProperties = $dom->getElementsByTagName('property');
    	//echo $pays->nodeValue;
 
		foreach($listeProperties as $theProperty){
			if ($theProperty->getAttribute("name") == "role") {
 
				$theRole = $theProperty->getElementsByTagName("value");
				foreach($theRole as $myRole){
					if($myRole->firstChild->nodeValue == "100.0"){
						$michaelPresence = "ONLINE";
					}
				}
			}
		}
 
  }

Post to Twitter

Your Comments

2 Comments so far

  1. Mihai Corlan says:

    Cher Michaël,

    Indeed there are many options to deal with XMP-RPC in PHP. I for one I prefer working with graphs of objects (or associative arrays) instead of XMLs.

    So back in 2007 when I developed an AIR app for our office, I used this library:

    http://www.keithdevens.com/software/xmlrpc/

    truly your team mate,
    Mihai

  2. VICTOR ZUNIGA MARADIEGUE says:

    Where is the source code???
    Regards


Share your view

Post a comment

Who am I ?

I'm Michaël CHAIZE, Adobe Flash Platform Evangelist based in Paris. I'm a big fan of Rich Internet Applications and I promote the Flash Platform in the Enterprise world.
You can follow me on twitter: http://twitter.com/mchaize

Magazine

Follow us on Facebook

© 2018 RIAgora. Powered by WordPress.

Daily Edition Theme by WooThemes - Premium WordPress Themes