Using ColdFusion with Flex – Part 4: Creating and running a session tracker
Important note: Effective with the release of Adobe LiveCycle ES, the Adobe Flex Data Services 2 server product has been rebranded as a Solution Component of LiveCycle ES. This article was written based on Flex Data Services but will likely work as is with LiveCycle Data Services ES. Any articles referring to or using ColdFusion and Flex Data Services are not compatible with LiveCycle Data Services ES. To learn about the new capabilities of LiveCycle Data Services ES, see the tutorials in the LiveCycle Developer Center and read about Adobe LiveCycle Data Services ES.
Adobe ColdFusion MX 7.0.2 enables you to create applications with the rich Internet capabilities of Adobe Flex and the advanced data retrieval features of ColdFusion. You can use ColdFusion to create, read, update, and delete records in a database while using the rich presentation features of Flex. This article is part of a series of articles that demonstrate how you can use ColdFusion with Flex.
This article uses the ColdFusion Session Tracker, a simple application used to track ColdFusion session activity in real time, to demonstrate:
- How to send structured data from ColdFusion to Flex clients using Flex Data Management Services
- How to subscribe to message channels and how to consume received data
- The use of custom grid renderers
- How to use states to implement expand/collapse views within grid cell renderers
This application is not intended to demonstrate development best practices. It was designed purely to demonstrate integration capabilities, functionality, and techniques.
Note: This application requires that you define an event gateway in the ColdFusion MX Administrator. Because of this, you cannot run it using ColdFusion Standard, which does not support event gateways.
Requirements
To make the most of this article, you need to install the following software and files:
Important note: Effective with the release of Adobe LiveCycle ES, the Adobe Flex Data Services 2 server product has been rebranded as a Solution Component of LiveCycle ES. This article was written based on Flex Data Services but will likely work as is with LiveCycle Data Services ES. Any articles referring to or using ColdFusion and Flex Data Services are not compatible with LiveCycle Data Services ES. To learn about the new capabilities of LiveCycle Data Services ES, see the tutorials in the LiveCycle Developer Center and read about Adobe LiveCycle Data Services ES.
Flex Builder 2 (SDK Included)
Flex Data Services 2
Flash Player 9
ColdFusion MX 7.0.2 (Includes connectivity for ColdFusion with Flex 2)
Sample files:
Prerequisite knowledge
Familiarity with ColdFusion data management and general Flex concepts.
Additional documentation
Installing and configuring the application
The Session Tracker application has two parts: the Flex code (MXML and AS files) and some simple ColdFusion test files (an Application.cfc and test.cfm pages). To install the Session Tracker application, follow these steps:
- Download and unzip the Session Tracker sample files
- Enable the ColdFusion adapter in the messaging-config.xml file (which is located in C:\fds2\jrun4\servers\default\samples\WEB-INF\flex if you used the default Flex Data Services installation). Ensure that the ColdFusionGateway destination and the
cfgateway adapter definition are not commented out. If you change the messaging-config.xml file while Flex Data Services is running, you do not need to restart the server; the change is detected automatically.
- Extract the Flex files from the ZIP file in a work folder under the Flex Data Services server. If you are using a default installation, you may want to use the Samples context and create a project folder named SessionTracker under C:\fds2\jrun4\servers\default\samples.
- (optional) Create a Flex Builder project to test the application, and specify that you are using Flex Data Services in the project wizard.
- Run the Session Tracker application, either from within Flex Builder or by browsing to it. The application displays an empty grid, which remains empty until data is received from ColdFusion. If connected and subscribed are both true in the status bar at the bottom of the screen, Session Tracker is working properly and is connected to Flex Data Services (awaiting inbound messages).
- Extract the CFC component files to the <webroot>/CFIDE/samples folder. (Create the samples folder if it does not exist.) After extracting the files, the directory structure includes /CFIDE/samples/SessionTracker.
- In the ColdFusion MX Administrator, define a new gateway instance named Flex2CF of type Flex Messaging. Point it to the Application.cfc file; you don't need a configuration file.
- Ensure that the Flex2CF gateway is running.
- Copy the three ColdFusion files into any folder under your Web root and browse either to the file1.cfm or the file2.cfm file. The session status appears in the DataGrid in Session Tracker. You can click the plus (+) symbol (or the session ID) to expand the session item to see session activity details.
The ColdFusion back end
The ColdFusion back end contains three files:
- test1.cfm and test2.cfm: test1.cfm and test2.cfm are simple test pages. You can edit these files or create your own test application files.
- Application.cfc: Application.cfc contains code for
onSessionStart, onRequestStart, and onSessionEnd. Each of these calls an internal statusChanged method, which constructs a message packet that contains information about the change and then sends the message to the gateway using the sendGatewayMessage() function.
ColdFusion sends messages to the gateway, but there is no tracking to ensure that the process worked or that the application sent the data. (It is best not to include that overhead in an Applciation.cfc file.)
The Flex client
The Flex Client contains five files:
- Main.mxml: Main.mxml is the main application page. It creates the DataGrid component, defines the connection to Flex Data Services, and initiates the subscription. When messages are received, a message handler passes them to CFSessions object for process, and then forces bindings to be updated.
- GridRenderer.mxml: GridRenderer.mxml is the renderer that your application uses to display the grid contents. It also defines an expanded state which, when displayed, embeds a nested DataGrid that contains session tracking details.
- CFSessions.as: CFSessions.as defines the ColdFusion session tracking class.
- CFSession.as: CFSession.as defines the individual session class, an array of which is defined in CFSessions.
- CFRequest.as: CFRequest.as defines the class that stores session details.
Where to go from here
You can use Flex Data Services to push data from ColdFusion to Flex clients, without needing client-side refreshes or timers. After you have defined a gateway and both ColdFusion and the Flex client are connected to the same Flex Data Services channel, pushing data from ColdFusion is simply a matter of using a single CFML function.
About the author
Ben Forta is the Adobe senior product evangelist and the author of numerous
books, including ColdFusion
Web Application Construction Kit and its sequel Advanced
ColdFusion Application Development, as well as books on SQL, JavaServer Pages,
WAP, Windows development, and more. Ben co-authored the official ColdFusion
training material, the certification tests and Macromedia
Press study guides for those tests, and now spends a considerable amount of
time lecturing, speaking, and writing about application development worldwide. Visit Ben's blog to read his regular postings on ColdFusion and more.