Accessibility

ColdFusion Article

 

ColdFusion and Flex: Do you need Flex Data Services?


Ben Forta

Ben Forta

forta.com

Created:
5 September 2006
User Level:
Intermediate

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 Flex 2 has been out for a couple of months now, and lots of ColdFusion customers are discovering that the combination of ColdFusion and Flex allows them to deliver powerful and engaging applications easier than ever before.

One area of confusion appears to be the Adobe Flex server: Adobe Flex Data Services. What is Adobe Flex Data Services? What exactly does Flex Data Services allow you to do that you wouldn't be able to do without it? What does this mean specifically to us ColdFusion developers?

Requirements

You may want to download and install the following software to follow along with this article:

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.

ColdFusion MX 7.0.2 (includes connectivity with Flex 2)

Flex Builder 2 (contains Flex 2 SDK)

Flex Data Services Express

The ColdFusion/Flex relationship

To get started with ColdFusion and Adobe Flex 2, all you need is ColdFusion MX 7.0.2 (which includes Macromedia Flash Remoting) and Adobe Flex Builder 2 (which contains Adobe Flex 2 SDK). You'll be able to build what are essentially client-server applications, where the client is Flex-generated Flash and the server is ColdFusion. On the server side, you can create ColdFusion components (CFCs) that use CFML and ColdFusion services; and on the client side, you can use MXML and ActionScript to build the Flash user interface.

To understand the relationship better between ColdFusion and Flex (including Flex Data Services), I'll start by clarifying what all of the bits are and what they do:

  • ColdFusion is obviously the back end. Although primarily used to power web applications, ColdFusion is actually not tied to the web; you can use it to power other applications, too (including SMS and IM applications). If you are a ColdFusion user, you will need ColdFusion MX 7.0.2 to effectively power your Flex 2 applications.
  • Adobe Flex Builder 2 is an Eclipse-based IDE you use to write and build Flex applications. In Flex 2, you write code (MXML and ActionScript) and then compile it, generating a Flash application (a SWF file) that you can deploy on your HTTP server, just like other assets (HTML files, GIF images, CSS files, and so forth). The generated and deployed SWF file can communicate with the server through an HTTP call, Flash Remoting (or AMF), and web services (or SOAP).
  • Flex 2 SDK is free with Flex Builder IDE, and allows you to compile MXML and ActionScript into an executable SWF file (which can also communicate with ColdFusion).
  • Flash Remoting is the communication mechanism used to transfer data between a back-end server and a Flash application that runs on a client in Adobe Flash Player. While Flash Player, and thus the Flash applications, can also invoke web services and make HTTP calls directly, Flash Remoting calls generally are preferred because they are more efficient and capable.

    To use Flash Remoting, your Flash application—including those created using Flex—must talk to a server with Flash Remoting installed on it. Flash Remoting has been included in ColdFusion for quite a while now, and ColdFusion MX 7.0.2 includes the latest version of Flash Remoting. This is also the version used in Flex 2 applications.

The client application uses Flash Remoting through the <mx:RemoteObject> tag to invoke ColdFusion component (CFC) methods, both retrieving data from ColdFusion and passing data from the Flash client to ColdFusion for processing. To make the integration as simple as possible, data types are preserved and automatically converted to their appropriate equivalent types. If ColdFusion returns a string, Flash receives a string; if ColdFusion returns a query, Flash receives an ArrayCollection, and so on. In fact, even classes can be maintained. Flash Remoting can automatically convert between ActionScript classes and ColdFusion components, allowing these to be passed back and forth efficiently and simply. One of the ColdFusion Flex Builder extensions allows you to right-click an ActionScript file to create the equivalent CFC, and vice-versa.

As I just explained, Flex applications can invoke ColdFusion back-end code. What is key here is that it is the Flash application that has to invoke that back-end code. All back-end interaction is client-triggered, just as it is in web pages. When a user clicks a button, it can trigger a call to ColdFusion, as could a menu selection, a screen change, or any other event. ColdFusion cannot push data down to the Flash client, nor can ColdFusion know anything about what is happening within the client unless the Flash client invokes a ColdFusion method to pass it that information.

So what can you build using this type of interaction? All sorts of things:

  • Display database front ends in an <mx:DataGrid>, even allowing users to edit and update the database content. Populate the grid with a call to a CFC method and send changes to another CFC method for processing.
  • Use Flex form controls to collect data, which you can then send to ColdFusion (perhaps when a user clicks a button) for processing.
  • Implement user authentication by challenging the user for identification (perhaps your application requires a log-on name and password) and then passing that data to a CFC method that returns true or false, depending on whether or not the user has been authenticated.
  • Use Adobe Flex Charting Components to chart result sets obtained from ColdFusion.

In all of these examples, a client-side event triggers a CFC call, passing and receiving data as needed. And for many ColdFusion-powered Flex applications, that may in fact be all you need.

But you may want more.

Understanding Flex Data Services

So far I have discussed Flex Builder, a client tool. There is also a Flex 2 server, called Flex Data Services (FDS for short). It gets installed and deployed on top of a J2EE server (possibly the same server running ColdFusion if you are using ColdFusion Enterprise and a J2EE deployment). Using FDS, you can compile MXML and ActionScript code on the fly (much like how you did in Flex 1.x). What makes FDS really compelling is the fact that it allows for a real bidirectional connection between the server and the client-side Flash application.

What does this mean? Consider the following scenarios:

  • Your charting applications (powered by ColdFusion-supplied data) need to display live data with changes to that data as they occur. You could use a timer in Flash and poll for new data every n seconds, but what you really want is for the server to just push new data to the client whenever there is any.
  • Your <mx:DataGrid> displays results from a database search, but that data could be stale and you would never know it. What you want is for data changes to be immediately reflected on the screen of any clients that happen to be looking at that data (and not those who are not, obviously).
  • Your database front end allows users to insert, update, and delete database records. Your old web version of the application allowed users to select a record for editing and then submit it to be saved. But what if two users edited the same record at once? Most likely, you just save changes as save requests occur, so the last update always wins. That may (or may not) be appropriate in a web application that displays a single record and refreshes pages frequently. But in a client-server type application, where lost rows may be redisplayed at once and pages never really refresh, the chances of overwriting on someone else's edits are high. An ideal way is to manage data synchronization and conflict resolution.

If any of these scenarios resonate, then you've just defined a need for Flex Data Services. As I already explained, Flex Data Services allows for open connections between client and server. There are two primary benefits to this:

  • Server-to-client push: The ability to push data to the SWF file running in Flash Player without needing to use timers and polling. For example, you could do one of the following:

    • Push real-time pricing data in an auction application
    • Push real data numbers to update charts dynamically
    • Push chat window text in a peer-to-peer (via the server) application
    • Notify a <mx:DataGrid> to update a specific row with changed data

    . . . you get the idea.

  • Data synchronization: This is the ability to safely handle concurrent access to back-end data, tracking data changes, and safely handle conflict resolution. No more last-edit-overwrites-any-prior-edit mishaps; you can write code that ties in to back-end data and talks to the Flash client, providing real end-to-end data synchronization. Instead of the client talking to a CFC directly that in turn talks to a back-end database, when using FDS and data synchronization, the client talks to FDS, which in turn talks to a back end (ColdFusion and the database). FDS, which sits in the middle, can arbitrate requests, monitoring for conflicts and alerting the client of conflicts when they occur.

ColdFusion and Flex Data Services integration

While most of the FDS demos and examples seem to concentrate on the push aspect of FDS, it is data synchronization that is likely of greater interest to most of us ColdFusion developers—considering that most of the applications built in ColdFusion tend to be highly data-centric database front ends.

While FDS is primarily targeted at Java/J2EE development, the ColdFusion team has done a considerable amount of work to expose powerful FDS functionality to ColdFusion developers:

  • ColdFusion developers can quickly and easily leverage FDS data synchronization capabilities through one of the Flex Builder ColdFusion Extensions that let you right-click a table (in the RDS Data panel) to launch a wizard that will write the FDS connectivity ColdFusion components for you. You'll still need to write some code—for example, what to do when a conflict occurs—but the ColdFusion Extensions do much of the heavy lifting for you automatically.
  • The CF-FDS adapter that connects ColdFusion to FDS is included in all editions of ColdFusion MX 7.0.2, not just the Enterprise edition. You can use it with all editions of Flex Data Services, including the free production version of Flex Data Services Express. (However, the gateway used to push data from ColdFusion to Flash via FDS requires ColdFusion Enterprise edition, as the interface is through an event gateways).
  • You can use Flex Data Services Express (limited to a single CPU) free of charge, allowing you to get started with FDS without additional cost.

Where to go from here

You can get started building ColdFusion-powered Flex 2 applications using just ColdFusion MX 7.0.2 and a Flex compiler (Flex Builder 2 or the free Flex 2 SDK). For applications that require data synchronization or server-to-client push, the best approach is to add Flex Data Services to the mix. And with Flex Data Services Express, available at no cost, there is no reason not to try FDS.

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.