Consuming the CMX Web Service with Flash MX and Flash MX 2004

Community MX publishes a web service that will display information about the latest content available from the site. This service can be used by anyone who knows how to consume a web service. Content of Community MX is copyright © 2003 by Community MX and cannot be redistributed, however you may use the web service on your own site to show a list of what is available on CMX. This article will be in two parts. The first part will show a simple retrieval of content using Flash Remoting.

A Web service is a service that is created to communicate, using standard XML formats, to any application that knows how to talk to it. The Web service will have a public interface written in Web Service Description Language (WSDL). The interface describes what the Web service does and how to communicate with it. The actual communication is done with XML, typically using the SOAP protocol. Because XML is basically just text, an application that can read text can read and interpret a Web service. Because of this, Web services can be created by one type of server and utilized by anyone who can read the XML.

Flash MX has made working with Web services even easier with Flash Remoting. A Flash Remoting developer can call a remote web service by allowing the ColdFusion or ASP.NET server to automatically create a proxy to the web service. To use Flash Remoting, you will need the Flash Remoting components installed into Flash MX. You can obtain these at http://www.macromedia.com/software/flashremoting/downloads/components/. If you have not worked with Flash Remoting before, check out the introductory article on Flash Remoting at Community MX.

Flash MX Pro 2004 contains built-in SOAP translation for web services, but there is a huge amount of overhead when using SOAP from a client. A typical SOAP call and response is 4 times larger or more that a typical Flash Remoting call and response. For that reason, it is better to allow the server to parse the huge SOAP packets, and just return what you need to the client in a small AMF (Flash Remoting) binary packet. Also, web services in Flash Pro will only work if they originate on your own domain, or have a policy file set up with the service provider. Using Flash Remoting, you do not have to worry about the policy file, as Flash Remoting can work with web services on any domain.

The Community MX web service utilizes a standard .wsdl file that can be consumed by any technology that can consume a web service. The web service is located at http://www.communitymx.com/services/cmxfeed.wsdl. The web service exposes 4 methods:

*not implemented yet

Using getContent

The getContent method simply returns the latest content to your service without any kind of filtering, or filtering by contentType only. It is essentially a dump of the the latest content descriptions from the site. The next section will show how to consume the service using Flash MX.

Step by step:

  1. Open Flash MX to a new file. You can name the file example1.fla.
  2. Create a Text element on the page and set the following properties in the property inspector:

    Size: 400x300
    HTML: True
    Type: Dynamic text
    Name: content_txt
    Multiline: true

  3. If you are using Flash MX and not Flash MX 2004, you'll have to add a scrollbar to the text field. Simply drag one from the Components panel and drop it on the text field.
  4. Open the Actions panel and make sure it's set to Expert mode (Flash MX only). Add this code for initializing Flash Remoting:
  5. // Include the required classes for Flash Remoting
    #include "NetServices.as"

    // Set up variable for the Flash Remoting URL
    var myURL = "http://localhost/flashservices/gateway";
    // Set up variable for the Community MX wsdl descriptor file
    var servicePath = "http://www.communitymx.com/services/cmxfeed.wsdl";

    // Connection hasn't been initialized; create connection and service objects
    if (initialized == null) {
      initialized = true;
      NetServices.setDefaultGatewayURL(myURL);
      var myConnection_conn = NetServices.createGatewayConnection();
      var myService = myConnection_conn.getService(servicePath);
    }

The location of the Community MX .wsdl file is at http://www.communitymx.com/services/cmxfeed.wsdl. As you can see, this is set in the code. The path to the .wsdl file becomes our service path in Flash. This will cause the ColdFusion server (or ASP.NET server) to generate a proxy for the web service on the server.

  1. The next step is to add the Flash Remoting responder object to handle all responses and error messages from the server. The server will pass an array of objects back to the caller. The objects contain the following fields:

    AUTHOR
    CATEGORY
    DESCRIPTION
    KEYWORDS
    TITLE
    TYPE_DESCRIPTION
    URL

    Responder objects can take many forms, but I like to handle results with a custom object, named SimpleResult here:

// Define the custom SimpleResult class to display the results
function SimpleResult() {
}
// Set up onResult() and onStatus() handlers as methods of SimpleResult class
SimpleResult.prototype.onResult = function (myResults) {
  var temp = "";
  var theLength = myResults.length;
  for (var i=0; i < theLength; i++) {
    temp += makeLink(myResults[i].TITLE,myResults[i].URL) + "<br>";
    temp += "Author: " + myResults[i].AUTHOR + "<br>";
    temp += "Category: " + myResults[i].CATEGORY + "<br>";
    temp += "<br>";
  }
  content_txt.htmlText = temp;
};

SimpleResult.prototype.onStatus = function (myError) {
  content_txt.text = myError.description;
};


_global.System.onStatus = SimpleResult.prototype.onStatus;

The onResult method of the SimpleResult object will handle all results from the call to the web service. It makes use of a utility function, makeLink(), to create a link from the TITLE field and the URL field. We are also using the AUTHOR field and the CATEGORY field in the results. You are also free to use the other fields that are not being used here.

We are also creating an onStatus method to handle any errors from the remote call, and also creating a handler for the _global.System.onStatus method, which usually indicates a failure to communicate.

  1. Next, we'll add the utility function, makeLink():

// Make a clickable link out of some text, given the text and a location
function makeLink(theText,theLink) {
  var temp = '<font color="#00cc00"><a href="';
  temp += unescape(theLink);
  temp += '" target="_blank">' + theText + '</a></font>';
  return temp;
}

  1. Lastly, we'll call the service directly, using an instance of the SimpleResult class to handle the results. When you place the responder object as the first argument in your call to the remote service, Flash will strip it off and use it as the responder object, and send only the 2nd through Nth arguments to the remote service:

content_txt.text = "...working";
// Call the service on load
myService.getContent(new SimpleResult(), "all");

The results can be seen here.

More information about Flash Remoting can be found at http://www.flash-remoting.com/ and at Macromedia's site. The next part of this series will show how to use the searchContent method, and also how to tie some of the new Flash MX 2004 components to Flash Remoting.