Feb 11 2008

Server push with Ruby on Rails using Flex, JRuby, and BlazeDS

Tag: ActiveMQ, ActiveMessaging, Flex, Flex and Rails, JRuby, Messaging, Server Push, blazedsDerek Wischusen @ 5:32 am

In a previous post I discussed how you can integrate Flex and Scala/Lift using BlazeDS.  In this post I’ll show that it’s also possible to get Rails running on BlazeDS using JRuby.

Here is what I am using for this example:

You don’t really need Netbeans, but I’ve found that it makes things a bit easier.

For this example I am going to assume you have a basic understanding of Flex and Rails.  That said, if anyone has a question, not matter how basic, please feel free to ask.

Here are the source files if you want to follow along:

—– 

Ok, first lets get the Rails app set up.

1. Create a new Rails app called RoRBlaze.

2. Generate a controller called Greeting with an index view.  i.e.,

> ruby script/generate controller Greeting index

3.  Open up your newly generated greeting.rb file and add the following code:

class GreetingController < ApplicationController
  include Java
  import "flex.messaging.MessageBroker"
  import "flex.messaging.messages.AsyncMessage"
  import "flex.messaging.util.UUIDUtils"

  @@msgBroker = MessageBroker.getMessageBroker(nil)
  @@clientID = UUIDUtils.createUUID()

  def hello
    @msg = AsyncMessage.new()
    @msg.setDestination("notifications")
    @msg.setClientId(@@clientID)
    @msg.setMessageId(UUIDUtils.createUUID())
    @msg.setTimestamp(Time.new.to_f)
    @msg.setBody("Hello from Rails")
    @@msgBroker.routeMessageToService(@msg, nil)
    render :text => "Greeting Sent"
  end
end

4. Open view/greeting/index.rhtml and add a button to it like so,

<%=button_to "Send Greeting to Flex", :action=>"hello" %>

5. Go to the BlazeDS folder and  locate the following two .jar files in tomcat\webapps\blazeds\WEB-INF\lib:

  • flex-messaging-common.jar
  • flex-messaging-core.jar

6. Copy these .jar files into the lib folder in your Rails app (keep this folder open, you’ll need the more of the .jars in a moment).

7. In your Rails app open up config/environment.rb and add the following to the bottom of the file:

Dir["#{RAILS_ROOT}/lib/**/*.jar"].each do |jar|
  require jar
end

Alright, thats pretty much all of the Ruby code for this simple example.  Now we need to use the Goldspike Rails plugin to modify our Rails app so that it can run in a Java server (like Tomcat or Jetty or Glassfish, etc)

8.  From a command line, go to the root of your Rails app and install the Goldspike plugin:

> ruby script/plugin install http://jruby-extras.rubyforge.org/svn/trunk/rails-integration/plugins/goldspike

9. WARify you Rails app using the following rake command:

> rake war:standalone:create

Assuming that Goldspike did its job, you should now have a folder called WEB-INF in the root of you Rails app.  We need to add a few file to this folder to get BlazeDS working.

10. Copy the .jars from BlazeDS\tomcat\webapps\blazeds\WEB-INF\lib into RoRBlaze\WEB-INF\lib

11. Copy the folder BlazeDS\tomcat\webapps\blazeds\WEB-INF\flex into RoRBlaze\WEB-INF

12. Open up the web.xml file in blazeds\WEB-INF and merges its contents into the web.xml file in RoRBlaze\WEB-INF.  Click here to see what the final file should look like.

13. Open up RoRBlaze\WEB-INF\flex\messaging-config.xml and modify it so that it looks like this (click to view the file).  The key thing to note here is that we added a messaging destination called “notifications”.  This is the destination that the Greeting controller publishes to and it is the destination that our Flex app will subscribe to.

14. Open up RoRBlaze\WEB-INF\flex\services-config.xml and modify it so that it looks like this (click to view the file).  The key thing to notice in this file is that we defined a streaming channel called my-streaming-amf that BlazeDS will use to push messages over a persistent, streaming HTTP channel.

That’s it for the Rails app.  Now let’s create the Flex app.

—–

1. Create a new Flex Project (File -> New -> Flex Project)

2. Fill in the rest as follows:

  • Project name: RoRBlaze
  • Application type: Web Application
  • Application server type: J2EE
  • Use remote object access service: check this
  • Root folder: RoRBlaze  (the root folder of your Rails app)
  • Root URL: http://localhost:8080/RoRBlaze/
  • Context root: /RoRBlaze
  • Output folder: RoRBlaze\public
  • Output folder URL: http://localhost:8080/RoRBlaze

3. Open up RoRBlaze.mxml and edit it like so:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="vertical"
	creationComplete="consumer.subscribe()">

    <mx:Script>
        <![CDATA[
          	import mx.messaging.events.MessageEvent;

            private function messageHandler(message: MessageEvent): void
            {
                pushedMessage.text = message.message.body as String;
            }
        ]]>
    </mx:Script>

    <mx:Consumer
    	id="consumer"
    	destination="notifications"
    	message="messageHandler(event)"
    	channelConnect="trace(event)"
        channelFault="trace(event)"
        fault="trace(event)" />

    <mx:TextInput id="pushedMessage" width="300"/>
</mx:Application>

That’s it for the Flex app.  Let’s test everything out.

—–

1. Open a terminal and go to the root of your Rails app.  Then use Goldspike to boot up your Rails app in a Jetty server:

> rake war:standalone:run

In a few short seconds your Rails app should  be up and running.

2. Launch the Flex app.

3.  Open a browser and enter the following URL: http://localhost:8080/RoRBlaze/greeting

You should see a single button that says “Send Greeting to Flex”

4. Click the button.

Upon clicking the button you should see the browser display “Greeting Sent” and you should see “Hello from Rails” appear in the text input in your Flex app.

That’s it.  Assuming that everything worked properly, the Rails app should have pushed an AMF encoded message to Flex using a persistent streaming HTTP channel created by BlazeDS.

One importatant thing to take note off is that this technique could be useful to you even if you aren’t planning on using Flex/Flash as your presentation layer.  You could use a Juggernaut style approach and push messages from Rails to an embedded Flash player whose only job is to pass those messages along to the browser.

This example demonstrates a very simple way to integrate Flex, Rails, and BlazeDS.  I think the ideal way to integrate these technologies would be to use the ActiveMessaging plugin for Rails, since both ActiveMessaging and BlazeDS have JMS adapters which they could use to integrate with a more robust messaging system like ActiveMQ.  If I can find some time in the near future, and if there is any interest, I’ll do a follow up post on this.


Mar 05 2007

JRuby + RED 5 = Ruby + RTMP

Tag: JRuby, RTMP, Red 5Derek Wischusen @ 12:51 am

JRuby is, in my opinion, one of the most exciting projects out there. Primarily because it makes it possible for Ruby developers to take advantage of all the technologies that have been built in Java. In particular, it makes it possible to take advantage of one of the other most exciting projects out there: Red 5.

Red5 is an Open Source Flash Server written in Java that supports:

  • Streaming Audio/Video (FLV and MP3)
  • Recording Client Streams (FLV only)
  • Shared Objects
  • Live Stream Publishing
  • Remoting

Red 5 is, or very soon will be, a viable free, open source alternative to Flash Media Server. Most important of all to the Ruby community is the fact that Red 5 recently added a scripting framework that makes it possible to use it with Ruby (via JRuby), as well as Javascript (via Rhino), Python (via Jython), and Groovy. Though, even if there was no scripting framework in Red 5, it should now be relatively easy to do using a simple wrapper class in Java that loads your Ruby classes using the new scripting framework in Java 6.

To get started with Red 5 and Ruby, first download a copy of Red 5, then check out this tutorial by Paul Gregoire.

If you want to try to use Rails with from the scripting framework in Red 5, then be sure to read the ‘Gotchas’ at the bottom of this page.

More to come on this in future posts.


Propecia 1mg generic
Tramadol cheapest
Valium online overnight
Free cialis online
Viagra indian pharmacy
Propecia uk prices
Viagra in france
Phentermine buy australia
Viagra cheap no prescription
Where to buy viagra online
Xanax buy uk
Genuine viagra online
Order xanax cod
Phentermine cheap online
Prescription propecia
Phentermine hcl without prescription
Propecia generic online
Tramadol pharmacy
Authentic phentermine 37.5
Viagra online uk
Generic xanax xr
Viagra ordering
Cheapest generic viagra online
Tramadol online overnight
Buying viagra in london
Purchase phentermine without prescription
Viagra canada prices
Phentermine buy uk
Buy phentermine hcl 37.5 no prescription
10mg prednisone
Viagra professional online
Buy xanax overnight
Generic cialis tadalafil
Buy viagra from canada
Buy female viagra without prescription
2mg xanax no prescription
Buy pfizer viagra without prescription
Tramadol medication
Where to buy levitra online
Cialis medication
Valium online pharmacy
Phentermine canadian pharmacy
Where to buy cialis safely
Prednisone dosages
Xanax price per pill
Buy generic propecia uk
Propecia price
Xanax no prescription required
Cialis dosage 20mg
Viagra shop online
Blood pressure and prednisone
Best viagra dose
Viagra in the uk
Tramadol no prescription required
Xanax 1 mg dose
Xanax no rx
Cheap xanax for sale
Online valium without prescription
Prednisone 20mg side effects
Buy phentermine online no prescription
Viagra prescription cost
Cialis online canadian pharmacy
Propecia information
Generic cialis uk
Where can i buy viagra in the uk
Viagra super active
Generic propecia
Australia viagra prescription
Valium online uk
Cialis side effects
Tramadol free shipping
Buying cialis online without a prescription
Get viagra
Prescription viagra uk
Dosage of xanax
Levitra canada
Levitra us
Viagra no prescription online
Phentermine canada no prescription
Low price cialis
Canada pharmacy valium
Valium pill 10mg
Purchase tramadol without prescription
Buy cheap valium online
Order xanax online
Cheap generic viagra
Buying viagra online
Cialis cialis
Best way to buy viagra online
Propecia cost
Cialis order online
Where to buy viagra in england
Xanax bars side effects
Levitra online
Buying prednisone online
Propecia usa
Best price on phentermine
Valium cheapest
Order viagra without prescription
Prednisone online
Purchase phentermine online
Valium drug side effects
Cialis canada no prescription
Phentermine hcl no prescription
Order tramadol overnight
Buy propecia
Tramadol prescription online
Drug phentermine
Free cialis samples
Xanax for sale without prescription
Where can i buy viagra without prescription
Best viagra alternative
Cheap cialis soft tabs
Buying tramadol in uk
Viagra dosage information
Generic tramadol
Cheap phentermine without prescription
Valium no rx
Purchase cialis without a prescription
Viagra canadian online pharmacy
Xanax bars dosage
Valium online fast delivery
Purchase phentermine
Where to buy cialis online
Viagra tablets for sale
Overnight delivery viagra
Buy viagra uk no prescription
Phentermine 37.5mg
Buying levitra without prescription
Viagra 50 mg online without prescription
Buy xanax online without prescription
Purchase viagra online without prescription
Cialis 10mg side effects
Buy valium europe
Prednisone tablets 10 mg
Prescription viagra canada
Real phentermine without prescription
Tramadol for sale
Best price tramadol
Propecia cheap
Where to buy phentermine cheap
Buy phentermine online without prescription
Prescriptions for phentermine
Cheap cialis viagra
How to buy phentermine online
Cheap 37 5 phentermine
Order tramadol online overnight
Viagra generic
Levitra samples
Order tramadol online cod
Cheap levitra no prescription
Tramadol no prescription overnight delivery
Cialis price
Cialis 20mg side effects
Cheap tramadol cod
Buy xanax canada
Valium generic
Buy valium no rx
Ordering propecia from canada
Xanax 0.5 mg
Buy viagra online uk no prescription
Phentermine 37.5mg side effects
100mg tramadol effects
Cialis 20 mg dosage
Cheap xanax bars
Levitra purchase
Cheap cialis india
Viagra online shop
Viagra sale uk
Buy viagra in canada online
Phentermine 37.5 pills
Viagra lowest prices
Best levitra prices
Cheap 100mg viagra
Canada viagra no prescription
Cialis 20mg
Propecia best prices
Xanax 1mg
Order cheap viagra online
Buy tramadol hydrochloride
Overnight xanax delivery
Where to buy propecia in canada
Cheapest levitra
Tramadol cod delivery
Order phentermine online no prescription
Prescription free viagra
Best prices for cialis
Viagra online purchase in india
Viagra express delivery
Phentermine purchase online
Viagra pharmacy prices
No prescription cialis online
Buy tramadol hcl
Viagra in usa
Levitra on sale
Side effects of viagra
Buy generic valium online
Propecia ireland
Buy levitra
Buy levitra online canada
Buy phentermine no rx
Purchase tramadol online
Viagra cheapest
Phentermine diet pills without prescription
Buying xanax online without prescription
Buying viagra in new zealand
Overnight tramadol no prescription
Buy propecia cheap
Phentermine online uk
Levitra online buy
Generic xanax
Cost of viagra 50mg
Xanax online cheap
Viagra price canada
Valium from india
Buy generic cialis online
Fedex tramadol
Buy viagra in england
Cheapest place to buy viagra online
Viagra for sale online
Viagra 50mg side effects
Where to buy cialis without prescription
Buy phentermine 37.5mg online
Buy xanax 2mg no prescription
Buy generic phentermine online
Viagra canada online
Mail order phentermine
Get viagra prescription
Best price cialis
Generic levitra uk
Where can i buy viagra without a prescription
Buy cialis online from canada
Buy cialis in the uk
Cialis soft tabs online
Cheap cialis
Buy valium cheap online
Cheap cialis pills
Cialis over the counter
Generic viagra for sale
Cheap levitra uk
Order cheap phentermine
Cheapest online cialis
Valium 10 mg
Cialis uk sales
Prescription valium
Phentermine without a prescription
Cialis samples canada
Purchase xanax
Buy cialis viagra
Viagra canada mastercard