Tuesday, February 09, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Feb 2006 >>
S M T W T F S
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28        

Search

Categories
 • Acrobat (3) [RSS]
 • Adobe (88) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (215) [RSS]
 • Appearances (190) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1371) [RSS]
 • Data Services (33) [RSS]
 • Fish Tank (4) [RSS]
 • Flash (179) [RSS]
 • Flex (493) [RSS]
 • Home Automation (4) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (109) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (19) [RSS]
 • SQL (40) [RSS]
 • Stuff (532) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (161) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
February 1, 2006

Getting Started With ColdFusion And Flex Enterprise Services

Using Flex Builder 2 you will be able to build complete Flex applications powered by a ColdFusion backend. And you can build them even without using Flex Enterprise Services (FES for short). But having said that, your application may indeed benefit from FES. We'll be exposing additional FES functionality in future betas, but for now we have made one important FES capability as simple to use as you'd expect from ColdFusion. I am referring to the ability to push content down to the Flash client without needing the client to poll or refresh on timed intervals. For example, a real-time auction application with price changes reflected as they occur, or a charting application plotting data in real-time, or a management application that shows you the status of users and sessions within your application. FES refers to this functionality as "publish-subscribe", clients can subscribe to a thread and when data is published they receive it automatically. The ColdFusion hook into FES is via an event gateway (so yes, this will require ColdFusion Enterprise, and it will also work with the Developer Edition). When the ColdFusion/Flex Connectivity update is installed, a new gateway type called "FlexMessaging" will be added to ColdFusion. You can create instances of this new gateway type to communicate with FES (inbound and outbound). To help you get started, the following is the simplest possible example of ColdFusion Flex communication via FES, an example of ColdFusion publishing and Flex consuming. It creates a simple HTML form used to post messages which then appear in real-time in a Flex Text box. Here goes:
  1. This assumes that FES is installed and running (it should be configured and running before you start CF).
  2. If you have not yet done so, be sure to enable the CF adapter in flex-message-service.xml (in C:\fes2\jrun4\servers\default\samples\WEB-INF\flex if you used the default FES install), here is what one could look like (with commented out stuff removed):
    <?xml version="1.0" encoding="UTF-8"?>
    <service id="message-service" class="flex.messaging.services.MessageService"
    messageTypes="flex.messaging.messages.AsyncMessage">


        <!-- description of message-service configuration -->
        <adapters>
            <adapter-definition id="actionscript"
    class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"
    default="true" />

            <adapter-definition id="jms"
    class="flex.messaging.services.messaging.adapters.JMSAdapter"/>

            <adapter-definition id="cfgateway"
    class="coldfusion.flex.CFEventGatewayAdapter"/>

        </adapters>

        <destination id="ColdFusionGateway">
            <adapter ref="cfgateway" />
            <properties>
                <gatewayid>*</gatewayid>
                <gatewayhost>127.0.0.1</gatewayhost>
            </properties>
            <channels>
                <channel ref="my-amf"/>
                <channel ref="my-rtmp"/>
                <channel ref="my-polling-amf"/>
            </channels>
        </destination>
        </service>
  3. It also assumes that you have a gateway defined in CF Admin named "Flex2CF" of type FlexMessaging (no config file is needed, and you can point to any CFC), and that that gateway is running.
  4. The following is a simple CFM file with a form that self-posts and submits to the gateway for processing:
    <!--- If form submitted, send message --->
    <cfif IsDefined("FORM.message")>
        <cfset msg=StructNew()>
        <cfset msg.body="#FORM.message#">
        <cfset msg.destination="ColdFusionGateway">
        <cfset ret=SendGatewayMessage("Flex2CF", msg)>
        <cfif ret>
            Message sent.
        <cfelse>
            Message failed.
        </cfif>
        <br />
    </cfif>

    <!--- Form --->
    <cfform name="form1" action="#CGI.SCRIPT_NAME#">
    Message:
    <cfinput type="text" name="message">
    <br />
    <cfinput type="submit" name="sbmt" value="Send">
    </cfform>

    <!--- Set focus to form --->
    <InvalidTag>
    document.form1.message.focus();
    </script>
  5. Create a new project in Flex Builder, be sure to select YES when asked if the project will be using Flex Enterprise Services, and create the project under the Flex default server samples folder (C:\fes2\jrun4\servers\default\samples\ if you are using the default installation).
  6. This is the entire Flex app. It is a simple text box which displays the received messages. It also shows the connection and subscription state at the bottom (they should generally both always be true).
    <?xml version="1.0"?>
    <mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml"
    creationComplete="initApp()">


        <mx:Script>
        <![CDATA[
        import mx.messaging.events.*;

        public function initApp()
        {
            consumer.subscribe();
        }

        private function messageHandler(event:MessageEvent)
        {
            t.text += event.message.body + "\n";
        }
        ]]>

        </mx:Script>

        <mx:Consumer id="consumer" destination="ColdFusionGateway"
                message="messageHandler(event)" />


        <mx:Panel height="100%" width="100%"
                title="ColdFusionGateway Messages">

            <mx:Text id="t" height="100%" width="100%" />
            <mx:ControlBar>
                <mx:Label text="Connected={consumer.connected}
    Subscribed={consumer.subscribed}"
    />

            </mx:ControlBar>
        </mx:Panel>

    </mx:Application>
Try it out. You should be able to type messages in the CF page in your web browser, and they'll show up in the Flex app.

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
What does destination mean [destination="ColdFusionGateway"]? Is that the URL of the ColdFusion application? How do you tell flex where the server is?
# Posted By justin | 12/4/09 6:20 PM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved