Before proceeding, you need some basic understanding of the functionality available to you in the XMPP gateway and the ColdFusion Administrator API.
For the ColdFusion server to respond to Jabber IM traffic, you must create a ColdFusion Component (CFC) that serves as an instance of the XMPP gateway. This CFC listens for and responds to incoming messages. This may sound complex but ColdFusion MX 7 and the XMPP gateway make building the necessary CFC quite simple.
Available in the XMPP gateway is all the functionality you would expect from an IM client, which includes the following functions:
onIncomingMessage: called when the XMPP gateway instance receives an IMonAddBuddyRequest: called when users add the gateway instance’s Jabber ID to their buddy listonAddBuddyResponse: called when responses are received from the gateway instance’s requests to add users to its buddy list, and also when other users ask the gateway instance to remove them from its buddy listonBuddyStatus: called when there is a status change (e.g. from active to away) for buddies on the gateway instance’s buddy listonIMServerMessage: called when there is an error or status message broadcast by the IM serverFor this tutorial you only need to concern yourself with a single function: onIncomingMessage. This is where you will place the CFML code that receives IMs from other users, creates an appropriate response, and sends the response back to the user as an IM.
No tutorial would be complete without a "Hello Word" example, so as a very quick taste of how incredibly simple the code for responding to incoming IMs is, let’s take a look at "Hello Jabber!" You won't actually build this CFC in this tutorial, but you may want to experiment with this on your own after completing the tutorial. Within the CFC that serves as your instance of the XMPP event gateway (more on setting this up in a moment), the onIncomingMessage function would look like the following:
<cffunction name="onIncomingMesage" output="false">
<cfargument name="incomingIM" type="struct" required="true" />
<cfset var outgoingIM = StructNew() />
<cfset outgoingIM.BuddyID = arguments.incomingIM.Data.sender />
<cfset outgoingIM.Message = "Hello Jabber!" />
<cfreturn outgoingIM />
</cffunction>
This small amount of code is literally all it takes to have your event gateway instance respond to all incoming requests with the message "Hello Jabber!" Not terribly exciting I realize, but with this understanding in place you’ll find that building more sophisticated IM applications is just a matter of expanding on this basic principle. The gateway instance CFC receives an IM, completes the necessary behind-the-scenes actions based on the incoming message, gathers the appropriate data for the response, and returns the IM.
The other piece of this IM application is the ColdFusion Administrator API. The Administrator API is new to ColdFusion MX 7 and allows you to programmatically access and manipulate many of the settings that have traditionally only been available through the web-based ColdFusion administrator or through undocumented (and unsupported!) means. Using the Administrator API with the XMPP gateway, the ColdFusion server can reveal all sorts of information through IM. The ColdFusion server can even change server settings based on IMs it receives. The Administrator API exposes a great deal of functionality. Read the ColdFusion Developer’s Guide included with the ColdFusion documentation or browse the CFIDE.adminapi components using Dreamweaver’s CFC Explorer for additional information.
Accessing and manipulating data sources is the probably one of the most frequent undocumented uses for ColdFusion functionality. Now you’re going to create an instance of the XMPP gateway that will give you a list of the data sources on your ColdFusion server. Your dutiful IM bot will even let you create a data source simply by sending the server an IM. Useful? I’ll let you be the judge of that! If nothing else, it’s a great way to get you thinking about other possible uses of the IM gateway, as well as explore a cool party trick to show your friends who may not use ColdFusion (and if you haven’t convinced them yet, shame on you!).