The Flash RSS Reader

30 May

A few people have expressed curiosity in it so I thought I’d run through how its done. At this stage its really very simple but I have plans to extend its operation to automate more of the running of it.

Macromedia first introduced the ability for the SWF format to parse XML data back in the days of Flash 5 (2000? 2001?). It was pretty basic was pretty shoddily implemented as well. It was never quite resolved whether it was an issue with the actual actionscript events or the Flash Player failing to parse these events (my money was on the latter) but suffice it to say, whilst it was great to be able to introduce another area of flexibility to SWF, it wasn’t until the release of Flash MX and the version 7 player that SWF/XML integration became a more reliable process.

It was around that time that Actionscript Guru, Sam Wan released his RSSDataFactory object which made coding the integration of RSS feeds very easy. He claims it only works with RSS 0.92 but it works fine with RSS 2.0 also.

So, I turned to this RSS object to handle the laborious work of processing the RSS. It was originally built to handle just one feed so I had a few tasks to make it operational for multiple feeds. I also had the thorny issue of the infamous Flash Sandbox to negotiate. More on that later.

Customising the RSSDataFactory is a doddle. First of all I simply inserted another keyframe and moved the DataFactory code to the second keyframe and applied a universal stop(); action to all keyframes. So, now I had two frames to play in I could put all my top level links in frame one and the parsed RSS feeds would display on frame two. Easy.

In frame one then I simply listed all the feeds I was going to be utilising as buttons and coded them appropriately.

Now you may notice at this stage that I’m not calling the feed directly. This is due to the pernicious influence of the Flash Sandbox. The Sandbox is a security measure. It doesn’t allow movies or scripts from servers on domains other than the one the SWF exists on to be accessed directly from the SWF. So what we have to do is build a sort of proxy – a script that _can_ access remote files and, as this proxy script exists on our domain, we can access anything it returns. Problem solved. Now you can see why calling remote XML files is pointless – nothing would be returned due to the Sandbox – so instead I call the proxy script. And because I have multiple feeds, I append a name/value pair (....php?feed=1) to each separate button. I can then detect these in my proxy script and hence return the desired script:

Now we turn our attention to this proxy script (which I called ‘antisandbox.php’). What we’re doing here is detecting the incoming value of the variable ‘feed’ fetching the appropriate RSS feed and opening it up for our SWF to process.

There are a few ways to do this, notably using readfile() but they are mostly (and again, notably readfile()) badly insecure. The way to securely get the feeds is to use (in PHP anyway) CURL. CURL opens a decent, protocoled connection to the remote RSS script. Its syntactically very like using a session in that you initialise it, use it then close it:

I should really have used switch to do the evaluation of the feed variable as its quicker. Probably will implement that soon.

So now we’ve got our data, we have to parse it in. Thankfully all this is done in Sam’s excellent Factory. All I had to do was pass it the value of the proxy script with the right name/value pair.

The last line of this script

rssObj.parseFeed(_root.theFeed, this, "receiveRSSObject");

calls the SWF variable I set in the previous frame that specified which value to send to the proxy script (and thus which RSS feed to fetch).

These are quite interesting:

In fame two I built a movieclip template. This template contains an empty dynamic text holder and an empty URL variable. When the RSS feed comes in, depending on how many posts each author specified their feeds help, each one of these posts would duplicate the movieclip and send the title of each post in the feed and the correct permanent link to these posts. It also spaces out each newly generated movieclip below the previously generated one.

My last problem was the ‘back’ button. Going back a frame in Flash doesn’t affect whats in the frame you’ve just left so I had to clear these newly generated movieclips, clear theFeed and reset the whole shebang everytime someone clicked the ‘back’ button.

Note I simply hide the ‘old’ buttons off stage – this speeds up regeneration if a user wants to revisit a certain feed in the same session.

And thats it for now. I will be extending this to make my life easier. For now, if I want to add a new feed I have to edit the FLA and ‘antisandbox.php’ which is a bit of a pain. What I hope to do is write a plug-in for WordPress that will create an XML file from a link category in the WordPress database architecture. I can then use this XML file to auto generate the feed list in frame 1. The second half of this plug in will drag these details from the database and execute them in ‘antisandbox.php’.

11 Responses to “The Flash RSS Reader”

  1. Andy Hume May 30, 2005 at 02:02 #

    Nice one Kev – Thanks for including me in this funky RSS affair, it is very slick… not to mention damn cool.

    Which brings me to the re-design. Man were you right about being ‘very tired’ with the last version- this one kicks it off the table and into somebody’s pint glass.

    Nice work.

  2. Kev May 30, 2005 at 13:14 #

    I think it’s a good thing to try and ‘redeem’ certain technologies through appropriately useful means – Flash got a (deservedly) bad rap for awhile and I’ll fully admit I used Flash and promoted its use in a bad way.

    I do feel it has a role to play but that role must be to add value where appropriate rather than Flash for its own sake.

  3. jaffry May 31, 2005 at 14:56 #

    nice app and nice writeup.

    i’m just wondering whether RSSDataFactory (or your sandbox) does any caching or bangs every server every time a request is made.

    and if you’re using PHP already, why not use PHP to parse the external feeds instead of delegating it to flash?

    i’m just trying to gauge which would be better… i’ve used magpieRSS for a php+xhtml RSS reader and am thinking of making a php+flash version soon.

  4. Kev May 31, 2005 at 15:15 #

    Hi Jaffry :o)

    As you probably know, SWF’s cache requests horribly anyway – it gets so bad on heavy transfers that its sometimes a good idea to use a ‘cache killer’ i.e.

    myLoadVars.cacheKiller = new Date().getTime();

    That said, unless Sam put in a dedicated caching script (which from what I’ve seen he didn’t) then no, the feeds won’t be cached which is one of the reasons I moved the feeds off the Stage area rather than delete the holding clips. That way it would be quicker to get them back should the user re-request them.

    re: using Flash and not PHP to parse, you’re absolutely right I could’ve done but one of the goals here was to try and ‘redeem’ Flash a little bit and show people it could be useful. I was originally going to do it in AJAX but then I grew more and more intrigued with the idea of doing it on Flash to see how it would compare.

    Your idea of porting Magpie to Flash sounds very interesting by the way. Have you actually made a start or are you still just mulling it over?

  5. jaffry May 31, 2005 at 17:11 #

    hey kev,

    good idea with moving the dataclip offstage rather than deleting it.

    the magpie-to-flash port… well, i’m still thinking about it (there’s the day job that gets in the way of all the fun stuff). i’m just thinking about the pros/cons of RSS parsing on client or server side, and how to get the most ‘optimized’ mix of things.

    if i get off my ass and start doing something soon, i’ll be sure to let you know how its going!

    🙂

  6. Tom May 31, 2005 at 23:18 #

    I wrote a FlashRSS thing once but without the RSSDataFactory. Either I’m missing something or Flash has shit XML support, they really need to add support for XPath/XQuery.

    I’ll see if I have it anywhere and post the source, perhaps someone might find it funky.

    P.S, love the spell checker!

  7. Kev June 1, 2005 at 06:45 #

    Tom: Flash has shit XML support ;o)

    I’d like to see your script though. Always interesting to see how others approach problems.

  8. Ross Turner March 17, 2006 at 12:47 #

    I was wondering if you could sort me out with some sample files that I could have a look at and play about with, so that I can see how you have done it all.

    Cheers

  9. Kev March 17, 2006 at 12:50 #

    I’ve abandoned it now Ross in favour of an AJAX one but I think I still have the files somewhere. I’ll get them to you if I have.

  10. Ross Turner March 17, 2006 at 14:55 #

    thanks. that would be great if you could.

Trackbacks/Pingbacks

  1. Autism Blog Web Design Blog: » Using MagpieRSS To Create An AJAX RSS Reader - June 23, 2005

    […] When I recently redesigned this site, one of things that I got asked about the most was my Flash RSS parser. I built it in an attempt to try and ‘reedem’ Flash a little as it occassiona […]

Comments are closed.