Aug 25, 2007
Building your first Twitter mashup with SWX RPC
In fact, you can create your very first Twitter mashup in just four lines of code by following these simple instructions:
- Open the SWX Data Analyzer
- In Flash, create a new FLA.
- Create a new movie clip and give it the instance name loader.
- On the frame that has the loader movie clip, add the following script:
loader.serviceClass = "Twitter"; loader.method = "getPublicUpdates"; loader.debug = true; loader.loadMovie("http://swxformat.org/php/swx.php", "GET");
That's all you need to get the latest public timeline updates from Twitter into Flash (you can see that the data is being loaded if you look in the SWX Data Analyzer).
To display the text of the first update in Flash, trace out the value of loader.result[0].text.
A quick hack to do this is shown below. Create a TextField on Stage and give it the instance name status and add the following code to your script:
function onEnterFrame { status.text = loader.result[0].text; }
Notice that you didn't need to download or install anything. That's because SWX is native! It uses SWF files to store and exchange data.
That's all you need to start working with SWX and Twitter! No API, external classes, etc. are necessary! And that little snippet of code (which, by the way, fits on to a moo card) also shows you exactly how SWX works. It loads the data in a SWF file and the data is accessible the moment it loads as native Flash objects. No deserialization necessary.
Of course, if you want to host your own SWX gateway or develop and test offline on your own machine, you can download and install SWX to your own machine. That will also allow you to create your own service classes and APIs.
Note: The Flash IDE will give you a security sandbox warning when you run the above example but the application will run correctly (look in the SWX Data Analyzer or trace out the loaded data to check this.) This warning occurs because you are running the SWF in the Flash IDE. If you 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 (the recommended way is to) use the SWX ActionScript Library, as exlained below.
