SitePen Support
Comet Support by SitePen

Dojo 1.2 and Comet, Part 1

by Dylan SchiemannJune 18th, 2008

The upcoming Dojo 1.2 release, scheduled for July, is a significant improvement over previous releases for Comet application developers. In Part 1 of this multi-part series on the improvements you can expect with Dojo, I’ll focus on instant messaging since chat is, after all, the hello world of Comet. Future installments will focus on improvements to the dojox.cometd client, and on the use of Dijit, Dojo’s widget system, to easily create widgets that update in real-time from live data sources.

But first, I’ll begin with an extension to Dojo that is now available. AOL recently announced the launch of the aimdojo project, an extension to Dojo that provides everything a developer needs to connect to the AIM service.

The aimdojo project provides source code for authentication, presence indication, IM widgets, sound effects common in AIM sessions, emoticon handlers, and much more. The code base makes use of dojo.io.iframe and dojo.io.script for Ajax and Comet, and takes a very event driven approach as evidenced by the following set of available event listeners and callbacks:

//"listeners" propery is an array of listeners to wim callbacks.
//You can add a listener via aim.wim.listeners.push().
//Listeners can implement the following function names:
//  onError(dataObject): An error occurred. dataObject is the error. 
//        Could be a string or an object.
//  onNeedAuth(): The user needs to go to the AIM site to authenticate.
//  ononNeedConsent(): The user needs to go to the AIM site to consent 
//        to the web site to receiving sending information on their behalf.
//  onAuthCancel(): The user canceled auth.
//  onConsentCancel(): The user did not allow access to buddy list.
//  onConsentInvalidToken(): The consent screen might send this sometimes. 
//         It seems to happen when clicking "Always Allow". Not sure what it
//         means yet.
//  onAuthConsentReceived(): The response from an auth or consent 
//         window/iframe was received.  The response may or may not be valid. 
//         Use this to close down any iframes/dialogs opened for the 
//         auth/consent flow.
//  onTokenComplete(): Indicates an OpenAuth token was successfully 
//         retrieved.
//  onUserLoad(): User's identity has been received. Should be the first 
//         successful callback after start() is called. To get the user's info, 
//         check aim.wim.user.
//  onSessionStart(): The user's session has started.
//  onSessionEnding(): The user's session is about to end. Functions that 
//         handle this event should do their job quickly.
//  onSessionEnd(): The user's session has ended.
//  onBuddyListLoad(): User's buddy list has been loaded. use aim.wim.groups
//         and aim.wim.buddies to build up the buddy list UI.
//  onIm(dataObject): An IM has been received. dataObject properties:
//         senderAimId: String. Buddy's aimId.
//         message: String. HTML instant message.
//         autoresponse: boolean. True if this is an auto response IM.
//         timestamp: integer. UTC timestamp when message was sent.
//  onPresenceChanged(dataObject): If a value in the presence info for
//         a buddy changes, this event will be sent with the following data:
//         aimId: String. Buddy's aimId.
//         oldValues: hashmap of changed presence properties. The old 
//         values are in this object. See aim.wim.buddies[data.aimId]
//         for the new/current values.   
//    onTypingChanged(dataObject): The typing status has changed 
//         for one of buddies. dataObject properties:
//         aimId: String. Buddy's aimId.
//         status: TypingStatus (see wim docs).
//    onGroupRemoved(): the requested group was deleted.
//    onGroupRenamed(): the requested group was renamed.
//    onBuddyAdded(): buddy was added
//    onBuddyRemoved(): buddy was removed

The main functionality of this library is through the inclusion of the AimPanel, which includes the other dependencies:

dojo.provide("aim.ImPanel");
dojo.require("aim._AimTemplatedWidget");
dojo.require("aim.LayoutContainer");
dojo.require("aim.StackContainer");
dojo.require("aim.browser");
dojo.require("aim.AddBuddyCard");
dojo.require("aim.BuddyList");
dojo.require("aim.ImGroup");
dojo.require("aim.ImUser");
dojo.require("aim.wim");
dojo.require("aim.widget.Pane");
dojo.require("aim.widget.AimButton");
dojo.require("aim.settings");
dojo.require("aim.widget.Dialogs");

Beyond AOL’s additions, and the already existing Dojo Cometd chat demos, comes the addition of an XMPP client thanks to a contribution by Sun Microsystems. XMPP is the protocol that makes Jabber work. The XMPP chat is not yet in Dojo’s trunk, but will soon be there, including all of the features you would expect from an XMPP client, and solid xdomain support. We’ll soon have a more extensive blog entry detailing this addition. For now, here’s some example code:

dojo.provide("dojox.xmpp.xmppSession");
 
dojo.require("dojox.xmpp.TransportSession");
dojo.require("dojox.xmpp.RosterService");
dojo.require("dojox.xmpp.PresenceService");
dojo.require("dojox.xmpp.UserService");
dojo.require("dojox.xmpp.ChatService");
dojo.provide("dojox.xmpp.PresenceService");
 
dojox.xmpp.presence = {
	UPDATE: 201,
	SUBSCRIPTION_REQUEST: 202,
//	SUBSCRIPTION_REQUEST_PENDING: 203,
	/* used when 'ask' attribute is absent on a roster item */
	SUBSCRIPTION_SUBSTATUS_NONE: 204,
 
	SUBSCRIPTION_NONE: 'none',
	SUBSCRIPTION_FROM: 'from',
	SUBSCRIPTION_TO: 'to',
	SUBSCRIPTION_BOTH: 'both',
	SUBSCRIPTION_REQUEST_PENDING: 'pending',
 
	STATUS_ONLINE: 'online',
	STATUS_AWAY: 'away',
	STATUS_CHAT: 'chat',
	STATUS_DND: 'dnd',
	STATUS_EXTENDED_AWAY: 'xa',
	STATUS_OFFLINE: 'offline',
 
	STATUS_INVISIBLE: 'invisible'
}
 
dojo.declare("dojox.xmpp.PresenceService", null, {
	constructor: function(xmppService){
		this.session= xmppService;
		this.isInvisible = false;
		this.avatarHash = null;
		this.presence = null;
		this.restrictedContactjids = {};
	},
 
	publish: function(presence){
		////console.log("Presence::publish() ", presence);
		this.presence  = presence;
		this._setPresence();
	},
 
	/**
	<presence from='juliet@capulet.com/balcony'>
	  <x xmlns='vcard-temp:x:update'>
	    <photo>sha1-hash-of-image</photo>
	  </x>
	</presence>
 
 
	<presence>
	  <x xmlns='vcard-temp:x:update'>
	    <photo/>
	  </x>
	</presence>
 
	*/
 
	_manageSubscriptions: function(contact, type){
		if (!contact){return;}
 
		if (contact.indexOf('@')==-1){
			contact += '@' + this.session.domain;
		}
 
		var req = dojox.xmpp.util.createElement("presence",
			{to:contact,type:type},true);
		this.session.dispatchPacket(req);
 
	},
 
	subscribe: function(contact){
		this._manageSubscriptions(contact, "subscribe");
	},
 
	approveSubscription: function(contact){
		this._manageSubscriptions(contact, "subscribed");
	},
 
	unsubscribe: function(contact){
		this._manageSubscriptions(contact, "unsubscribe");
	},
 
	declineSubscription: function(contact){
		this._manageSubscriptions(contact, "unsubscribed");
	},
 
	cancelSubscription: function(contact){
		this._manageSubscriptions(contact, "unsubscribed");
	}

A feature-rich and fully-functioning dojox.xmpp implementation will be part of the Dojo 1.2 release.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Webtide

6 Responses to “Dojo 1.2 and Comet, Part 1”

  1. Emil Ong Says:

    These are some pretty exciting developments, Dylan!

    Is there more information about the XMPP implementation anywhere? I’m particularly wondering if it uses BOSH to connect to the server.

    Thanks!

  2. Jason Cline Says:

    Hi Emil,

    Yes the dojo xmpp lib does use BOSH over a regular XHR request. So in theory it will be able to connect to any BOSH compliant XMPP server. There is also an experimental cross-domain transport that uses the script src alternative syntax of BOSH: http://www.xmpp.org/extensions/xep-0124.html#script

    We will have the code checked into the dojo repository soon and there and documentation will follow as well.

  3. Rob Hayward Says:

    Hi guys,

    Great resource for keeping up with Comet developments. Dying to get involved in building some live apps however my server/networking knowledge is not the best. Has anyone any whispers or has used any Hosts that have experience setting up dedicated servers with ready fire/testbed comet apps?

    I don’t mind paying for the dedicated server but it’s not easy to setup figure out setting up the testbed but after that I would be flying as I am sure would many other front end developers.

    Anyone follow? Thanks, Rob.

  4. Rob Hayward Says:

    Apologies for the shocking grammar, I typed that a little too fast :)

  5. GregWilkins Says:

    Rob,
    at webtide, we are working with http://www.mor.ph to provide an easy deploy to grid experience for java webapp. They already well support ruby webapps. Our current work with them is to introduce better cometd support and we will soon have some announcements there and some demonstrations.

  6. DylanSchiemann Says:

    Hi Emil, check out http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/xmpp/

Leave a Reply



Copyright 2009 Comet Daily, LLC. All Rights Reserved