Working with SWX: The Native Data Format for the Flash Platform (Open Source Flash Development) Part 1

SWX is the native data format for Flash. Data is wrapped inside a SWF shell (Figure 8-1), or the most native possible format on the Flash Platform. The SWF is then interpreted by Flash Playerjust like a SWF is compiled using the Flash IDE. The SWX format is a subset of the SWF format, just like JSON is a subset of JavaScript.

Datum, the SWX mascot/logo. He lives in a SWF shell.

Figure 8-1. Datum, the SWX mascot/logo. He lives in a SWF shell.

For Flash developers, this means you can use Flash’s loadMovie() method to load SWX data files, and that data is ready to use the moment it has loaded. There is no need to deserialize or massage the data in any way before using it, unlike XML where developers must parse using firstChild, lastChild, and so on, and then convert the text returned into native Flash objects. Instead, with SWX the data is stored in native Flash objects such as a String, a Number, or a generic object.

SWX is pronounced “swix.”


Term

Definition

SWX

Native data format for Flash. This is a subset of the SWF format. SWX SWFs are regular AVM 1 * SWF files that are used to store data only.

SWX RPC

RPC protocol encoded in SWX format. The SWX RPC allows you to call methods on server-side classes and get the results returned in SWX format.

Term

Definition

SWX PHP

SWX RPC implementation in PHP. SWX PHP is the most common implementation of SWX RPC, but there are other implementations in various stages of development by the SWX community (Python, Ruby, J2EE, .NET, and so on). For the purposes of this chapter, all examples will be shown and discussed using SWX PHP.

SWX Tools

The SWX Service Explorer and SWX Data Analyzer. These tools come bundled with SWX PHP and will work with any future implementations of SWX RPC.

SWX AS

A high-level ActionScript library that handles the client-side queuing and execution of SWX RPC calls. Developers do not have to use the ActionScript library to work with SWX RPC because SWX is native to Flash, but the library does provide developers with useful functionality such as cross-domain access, queuing of data calls, and more.

SWX APIs

APIs for Flickr, Twitter, and so on. The SWX APIs are as important as the SWX data format and the SWX RPC, because they provide Flash developers with a very easy way to create mashups. Using the public SWX RPC gateway at http://swxformat.org, developers can use these APIs with ActionScript alone (no server-side programming is necessary).

Public SWX RPC gateway

Open SWX RPC endpoint. The public SWX RPC gateway is available for use at http://swxformat.org/php/swx.php, and all of the available services can be viewed using the SWX Service Explorer (http:// swxformat.org/php/explorer/). You can use the public SWX APIs from this gateway in your own applications without having to write any server-side code whatsoever.

* ActionScript virtual machine, version I (AVM I), is the code interpretation engine within Flash Player, which supports ActionScript versions I and 2. AVM 2 is found in Flash Player 9 and later and is required to interpret ActionScript 3 and later.

Why do you need a new data format?

When SWX was first released, there was a good amount of discussion about why a developer would need SWX. With all the other options for getting data into Flash, why should you use SWX?

■    Existing formats are non-native, are complicated, and require parsing and/or writing plumbing code such as XML (firstChild, lastChild), variable-encoded strings (LoadVars, loadVariables), Flash Remoting, and so on.

■    SWX files have low processor overhead when parsed by Flash Player, because they are native SWF bytecode.

■    SWX RPC is the only RPC solution for Flash Lite 2.0 and 2.1 (and thus for mobile Flash applications). Flash Remoting, such as AMFPHP, does not work with Flash Lite.

■    Most important, SWX, being native, has inherent advantages such as cross-platform data exchange (via allowDomain support for SWF files), simplicity (no ActionScript library is necessary to use it, although there is a high-level library if you want to use it), and so on.

It’s my first belief that every platform can benefit from a native data format that does not require parsing. The main advantage of SWX over other data formats and of SWX RPC over other RPC solutions is ease of use. I hope that this ease of use will spur a wealth of development of data-driven applications and mashups on the Flash Platform by developers who may traditionally have shied away from creating such applications because it was just too darn hard to do so.

Supported platforms and technologies

Developers can make the server-side call to create (and the ActionScript call to retrieve data from) SWX SWF files with any version of Flash (6+), because they are simply SWF files.

If developing with ActionScript 3 for Flash Player 9 and AVM 2 (see the Table 8-1 footnote), you cannot simply load the SWF returned from SWX, because AVM 1 and AVM  2 SWF files cannot see within each other to read the encapsulated data. Until an update is developed for SWX by the open source community, you can load the data returned from SWX only into a SWF published for ActionScript 2.0/AVM 1.

SWX PHP, the most popular current SWX RPC implementation, creates SWX files that are compatible with Flash Player 6+ and Flash Lite 2.0/2.1/3.0. Currently, SWX PHP does not support Flash 9/AVM 2, but it will eventually do so.

SWX AS, the SWX ActionScript 2.0 library, compiles on Flash 6+ and with MTASC. Future versions of SWX AS will also be developed in ActionScript 3, requiring Flash CS3 or Flex 2+ to compile.

Currently, you can use SWX to create applications that run on mobile phones (Flash Lite 2.0/2.1/3.0), the Nintendo Wii, Sony PlayStation 3, and devices such as the Nokia N800 Internet tablet and the Chumby.

You can also wrap SWX-based applications to create desktop versions using Adobe AIR by loading your completed AVM I SWF application into a Flash 9/AVM 2 SWF via the Loader class and loading the AVM I SWF just like any other external asset such as a JPG, PNG, or GIF. See http://livedocs.adobe. com/flex/3/langref/flash/display/Loader.html for more information.

How simple is SWX?

SWX is so simple that the instructions for getting data from SWX into Flash easily fit onto a Moo MiniCard (http://moo.com/products/minicards.php; see Figure 8-2).

Two SWX Moo MiniCards (both front and back views stacked on top of one another)

Figure 8-2. Two SWX Moo MiniCards (both front and back views stacked on top of one another)

To get a list of the most recent 100 photos from Flickr into Flash, do the following:

1.    Open the SWX Data Analyzer at http://swxformat.org/php/analyzer/.

2.    In Flash, create a new FLA, and create a MovieClip on the stage. Give it an instance name of loader.

3.    Write the following code on the main timeline frame that the loader MovieClip is on:

tmp9d3-32_thumb

That’s it! Test your movie and look in the SWX Data Analyzer to see the results being loaded into Flash from Flickr. That is how simple SWX is.

If you want to display the results from within Flash, create a long, single-line dynamic text field on the stage, and give it the instance name status. Add the following code to the timeline:

tmp9d3-33_thumb

Initially, the status text field will display undefined until the data is loaded, and then it will display the URL of the first photo from the list of recent photos that is loaded from Flickr. Notice how you can access the results as native Flash objects the moment they are loaded. No deserialization or massaging of the data is necessary!

The Flash IDE will give you a security sandbox warning when you run the previous example, but the application will run correctly. This warning occurs because you are running the SWF in the Flash IDE. Ifyou put the SWF file on the same domain as the SWX gateway, it will also work without requiring any further code. However, if you want to use the public SWX gateway and deploy your Flash applications to your own server, you must either manually call System.security.allowDomain or use the recommended fix, the SWX ActionScript library, as explained in the next section, “Diving deeper: SWX AS, SWX Service Explorer, SWX Data Analyzer.” v_J

This example used the SWX public gateway (http://swxformat.org/php/swx.php), which you are welcome to use in your own applications to create mashups with the supported APIs (Flickr, Twitter, and more) without having to host or write any back-end code yourself.

Diving deeper: SWX AS, SWX Service Explorer, SWX Data Analyzer

As you just saw, SWX can be as simple as six lines of code. However, there is a more powerful and developer-friendly way to work with SWX: SWX AS, which is the ActionScript API.

SWXAS

The SWX AS library provides developers with useful functionality, such as cross-domain access, queuing of data calls, fault and timeout handlers, and more.

You can download the SWX AS package from the SWX website, and it is included with all SWX bundles, except for the deployment bundle (which includes only the server-side files necessary to run SWX PHP).

There are two additional tools provided with SWX on both a custom or local deployment and on the publicly hosted version located at http://www.swxformat.org that help to increase the simplicity of testing and debugging your applications: the SWX Service Explorer and the SWX Data Analyzer.

SWX Service Explorer

With the SWX Service Explorer, you can view and test your service classes without having to create your own Flash client. It does this by providing you with the means to call specific methods of your services and then by consuming the SWF returned and allowing you to inspect the data contained within that SWF.

In a custom or local deployment, you can find the SWX Service Explorer by clicking the thumbnail link on the SWX Start Page, which in most normal installations can be found at http://localhost:8888/ start.php. You can also view the public installation of the Service Explorer available at http:// swxformat.org/php/explorer/.

Once you load the Service Explorer, you will see that several PHP service classes are listed (see Figure 8-3). SWX PHP ships with several APIs preinstalled, including Flickr, Twitter, and many others. These APIs are also available for usage on the public gateway on the SWX website, making it easy to consume data from these services and create mashups.

The SWX Service Explorer

Figure 8-3. The SWX Service Explorer

SWX Data Analyzer

The SWX Data Analyzer (see Figure 8-4) is a debugging tool that shows you the SWX data that arrives inside your Flash movie. I’ll discuss the Data Analyzer in more detail in the following examples.

In a custom or local deployment, you can find the SWX Data Analyzer by clicking the thumbnail link on the SWX Start Page, which in most normal installations can be found at http://localhost:8888/ start.php. You can also view the public installation of the Data Analyzer available at http:// swxformat.org/php/analyzer/.

The SWX Data Analyzer

Figure 8-4. The SWX Data Analyzer

Installing SWX on your server

Before installing SWX PHP, you will need to have a web server running PHP on the machine on which you are installing it. SWX PHP is written in pure PHP, does not require any extensions, and runs under both PHP 4 and PHP 5. As such, it runs without problems on shared hosting accounts.

For the purposes of this topic, we’ll focus on SWX PHP. However, there are several other implementations of the SWX RPC gateway in various stages of development for languages such as Python, Ruby, J2EE, .NET, and more. Check the SWX website (http://www.swxformat.org) for more information.

SWX PHP is a SWX RPC implementation in PHP. SWX PHP has a SWX RPC endpoint (called the gateway) and an assembler that creates and returns SWX files. Your service classes contain business logic only, meaning the assembly of the SWF that contains your data is handled for you. SWX PHP comes with service classes (APIs) for Flickr, Twitter, Ten Word Review, Nabaztag, and more, preinstalled as examples and/or ready-to-use services.

SWX PHP uses AMFPHP as a library, and as such, you can also utilize SWX PHP services via Flash Remoting, JSON, and XML-RPC. This means that you are not locked into using the SWX RPC, and you can switch easily between these various technologies if you want at any time.

SWX PHP comes with a plain-vanilla installer, so you can unzip it into your web folder on the server and be up and running. This is perfect for use on a hosted web server. However, if you want to run SWX locally and you do not have a web server and PHP installed, the following are some suggestions for getting a full-blown local web server with PHP installed as one package.

Mac OSX

If you’re on a Mac running OS X, download and install the SWX PHP MAMP bundle available at http://swxformat.org/download. This will have you up and running with everything you need, including SWX.

Windows

Install WAMP, install XAMMP, or manually install a web server and PHP. WAMP is available at http:// www.wampserver.com/en/. XAMMP is available at http://www.apachefriends.org/en/xampp.html.

Linux

Install XAMMP, or manually install a web server and PHP. XAMMP is available at http://www. apachefriends.org/en/xampp.html.

All platforms

Once you have installed a web server and PHP, download SWX PHP from http://swxformat.org/ download, and unzip it into    the web root of your server. Startyour web server, and hit the web root in your browser to access the SWX PHP Start Page (see Figure 8-5).

With the release of Flash Player 9.0.124, Adobe implemented additional security limitations, and as    such,    a properly configured crossdomain.xml file is required to allow full access to SWX. You can find more information, along with a link to an example crossdomain.xml file, at http://www.jonnymac.com/blog/2008/04/08/flash-player-90124-released-with-security-updates/.

The SWX PHP Start Page provides links to documentation, the Service Explorer, and the Data Analyzer, and it runs a few tests to ensure your SWX RPC gateway is running correctly.

Figure 8-5. The SWX PHP Start Page provides links to documentation, the Service Explorer, and the Data Analyzer, and it runs a few tests to ensure your SWX RPC gateway is running correctly.

Next post:

Previous post: