Tuesday 27. of January 2009
Tags:flex and rails, flex, rails, integrating flex and rails     By: By: Asad Awan
Posted in Flex, Ruby

Since the release of Flex, large numbers of developers have been attracted to the idea of creating powerful web applications that integrate Flex and RoR. Previously we reviewed such applications successfully integrating Flex and RoR, and today we will discuss a new technology that marries Flex with RoR applications by providing a means of automating the communication between client and server.

The technology we will use for integration is "WebORB for Rails." It is a free and open source (GPL) server made available by Midnight Coders.

Most of the articles available online are about integrating Flex clients with Rails using raw XML over HTTP. This level of integration is simple for the sake of a tutorial but soon it becomes bulky as the application grows more complicated. With each new operation on the server, a developer must spend valuable cycles serializing and deserializing requests and responses.

WebORB relieves the burden of this "serialization tax" by supporting the concept of remote objects, where Flex users can natively invoke methods on the server and retrieve the responses all within ActionScript, the object-oriented programming language used by Flex.

First of all we need to install WebORB for Rails. WebORB runs as a plug-in for Rails and is installed by simply running this command within the root directory of a Rails application on Windows:

ruby script/plugin install
http://themidnightcoders.net:8089/svn/weborb/

After Installation we need to write an Application Using WebORB. The steps that are required to build a Flex-based RIA utilizing Rails are.

  • Building the service class and drop into RAILS_APP/app/services
  • Adding an entry to RAILS_APP/config/WEB-INF/flex/remoting-config.xml for the remote service
  • Setting up a RemoteObject on the client-side using Flex that will communicate directly with the back-end service.

As a simple example we need to consider the following service written in Ruby:

require 'weborb/context'
require 'rbconfig'
class InfoService
def getComputerInfo( requestId )
computer_info = Hash.new
request = RequestContext.get_request
computer_info['serverName'] = request.server_software
computer_info['requestId'] = requestId
computer_info['os'] = Config::CONFIG["arch"].to_s
computer_info['currentTime'] = Time.now
computer_info
end
end

This example is bundled within the WebORB on Rails product and simply returns a bag of properties correlated to the server running the service. Normally, the classes that are surfaced as remote objects are added to a /services directory under the RAILS_APP/app directory.

After creating this class, we must also tell Flex that the class needs to be available as a remote object. This is configured by adding the following snippet of XML to the remoting-config.xml file located within the RAILS_APP/config/WEB-INF/flex directory:

 <destination id="InfoService">
<properties>
<source>InfoService</source>
</properties>
</destination>

In the end this service is invoked by a client by using the RemoteObject class within ActionScript. Following is a small chunk of code that exhibits how to use this class:

remoteObject = new RemoteObject();
remoteObject.destination = "InfoService";
remoteObject.getComputerInfo.addEventListener("result", onResult);
remoteObject.addEventListener("fault", onFault);

Now assuming that the remote object variable was declared at the top of our MXML application, this code illustrates creating and initializing a remote object for use. It should be noted in particular that the destination property is set to "InfoService", which maps directly to the ID attribute of the destination we just configured within remoting-config.xml. We also need to note that this is how Flex (and WebORB) determines where to send this request. The final two lines above demonstrate adding callback methods to the remote object; the first line tells Flex to pass the result of a call to the getComputerInfo method (this maps to the Rails service method on the server) to the onResult method (which is on the client). The last line tells the remoteObject instance to pass any faults to the onFault method (also implemented in ActionScript). Following Example shows the two event listeners that were registered above:

public function onFault(event:FaultEvent):void
{
Alert.show(event.fault.faultString, 'Error');
}
public function onResult(event:ResultEvent):void
{
var computerInfo:Object = event.result;
serverInfoText.text = computerInfo.serverName;
requestIdText.text = computerInfo.requestId;
osText.text = computerInfo.os;
timeText.text = computerInfo.currentTime.toString();
invokeButton.enabled = true;
}

Now the remote object can invoke the method on the server written in Ruby by simply calling:

remoteObject.getComputerInfo("TEST");

The results will be available in the event.result object that is passed to the onResult method as demonstrated above. In this example each property that is stored on the server is available as a property of the event.result object that we store above in the computerInfo instance. The source code for this example is available as part of the WebORB distribution for those interested in looking at the complete picture; after installing WebORB the server code is available in /app/services/InfoService.rb and the client code is available in /public/examples/example.mxml.

Conclusion:
This is an initial glance at how Flex clients can be integrated with Ruby on Rails using WebORB's Flex RPC implementation and the RemoteObject API. WebORB on Rails simplifies the development process as the amount of client/server interaction scales to a large number of request/response types.

 

References:

Derek Wischusen (2006). Integrating Flex 2 and Ruby on Rails. Retrieved Jan. 7th, 2009 from http://www.adobe.com/devnet/flex/articles/flex2_rails.html

You can leave a trackback from your own site.
Comments

Displaying results 1 to 5 out of 89
1 2 3 4 5 6 7 8 9 10 Next
 
   Charly groenendijk xanax. Sunday, 20-02-11 22:19
Charly groenendijk xanax.
   Cla. Saturday, 19-02-11 03:53
Cla.
   Celexa. Friday, 18-02-11 19:21
Celexa and hair loss. Celexa info. Celexa.
   Order tramal. Thursday, 17-02-11 10:30
Medication tramal.
   Psychics. Saturday, 15-01-11 16:16
Psychics.
1 2 3 4 5 6 7 8 9 10 Next

Leave a reply:

Name:
E-Mail:
Message: