-
Get a monthly update on best practices for delivering successful software.
I've just completed a project where I was the Rails developer for a site that was integrating a Flex application that needed to pull from a database. A primary consideration is how to transport the data. Rails supports xml and json natively, and is very easy to set up by adding a single line to a controller's respond_to method. Sasha, the Flex developer on the project, suggested that we go with AMF if possible as it's native to Flex and is deserialized straight into custom typed objects.
After reading a bit about some performance considerations it looks like AMF and JSON are going to get roughly similar performance in most cases, so it becomes a matter of ease of development and taste. Sasha definitely preferred working with AMF, so i started checking out Rails implementations.
The two main choices available are RubyAMF and WebORB for Rails. I had been working through Peter Armstrong's excellent book, Flexible Rails, in which he has a chapter about Rails and AMF support. Based on his recommendation, and the current activity level of the two implementations, I chose to go with RubyAMF. Peter makes it plain that his opinion is skewed as he is a active contributor to the RubyAMF code base, but argues that the reason he is a contributor is because it's the better implementation. Good enough for me to give it the first shot.
It turns out that it was a excellent choice. I was able to plug in and configure RubyAMF, and build a basic Flex app to test it in a couple of hours. After installing the RubyAMF plugin, the main points of interest in the ruby code.
The thing I appreciated most about the setup was the ability to map seperate attribute lists. The Flex app does an inital call to the index method of the Beers controller, but with a subset of attributes. All of the fields pulled when the app loaded where numeric or short strings. The Beer model contains two large text fields, but we made the decision to retrieve those when they were needed in a seperate call. The :show class_mapping_scope was used in the show method of the controller. The end result was that we could keep the size of the record set down when we where asking for hundreds of objects, and just get the larger text fields when we were asking for one record, avoiding any scenerio where we would have to transmit large text fields for a large number of records.
That is really about all that's needed on the rails side. On the flex side things are just as simple.
First setup a services-config.xml.
Next, set up the remote object ...
and the custom type object in Flex.
The call to the index method would look like
BeerService.index.send();
And the code from the event handler when the call returns..
var temp:Array = new Array;
temp = e.result as Array;
The cool part about this is that the objects in the array will be of custom type Beer with no client side manipulation needed.
If you're looking for a server side technology to communicate with a Flex application, the speed at which a Rails app can be developed and the ease of AMF integration makes a strong case to be the technology of choice.
Related posts:
I’m copiing/pasting a comment I have made in a previous Flex and rails blog post :
The example is really cool but what about a bigger Flex App.
I have been trying to build Flex front-end to a RoR webservice and this is pretty painful (event with RubyAMF).
Flex seems to be pretty poor and/or ugly when it game to build a whole application with a RESTFul back-end. (with more than one remote Object)
The solution could come from the Ruboss framework but from my early tests, it’s young and suffers from a big lack of documentation.
Have you got any good idea for drying Flex code in these cases.
Comment by jblanche, Thursday, November 6, 2008 @ 8:25 am
@jblanche – Flex is great, but if you’ve never used it before you’ll definitely end up writing obtuse code.
Try the Mate Framework – it is very good and definitely mature enough for production now.
Keep in mind that Flex is much more powerful than HTML/JS for a front-end, but it is also much more difficult. Be patient with it, or hire a Flex developer.
Comment by sean, Sunday, November 16, 2008 @ 2:18 pm
[...] a related post on RubyAMF and Flex from the Flex perspective , and Justin has written one on Rails, AMF and Flex from the Rails [...]
Pingback by Agile Ajax » Pathfinder Launches Beer Hunter, A New Flex + Ruby RIA » Pathfinder Development, Monday, May 4, 2009 @ 6:08 pm