Rails Listener
Trace: » 2.x Docs and Downloads » Welcome to Entourage! » Rails Listener

Rails Listener

Entourage MQ supports communication with standard Ruby on Rails resources by following some conventions. The Rails Listener routes messages with a standard signature to appropriate JSON services implemented using standard Rails controllers.

Sending Remote Messages

Messages are dispatched to a Ruby on Rails resource when they are prefixed with rails:. The message name format should be as follows:

<action>.<resource>.request

Where the action is the action you are calling (only index, create, show, update and destroy are currently supported) and resource is the pluralized resource name (i.e. articles not article).

For example to make a request for a resource of the type article with and id of 2 see the following code:

<script>
$MQ("rails:show.articles.request", { id:"2" });
</script>

All the data in the payload object is serialized to JSON and sent to the resource controller on the back end. When the message is received, it is passed to the controller in the params argument as usual with the params[:format] set to .json.

Handling the request in the controller

A typical scaffold generated controller serves html and xml, but not json. To get your controller to support the .json format you just need to add that to your respond_to block. See the following example:

def show
  @article = Article.find(params[:id])
 
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @article }
    format.json { render :json => @article }
  end
end

Handling responses

When a response is received from the server, MQ will publish a response message with the data payload specified by the server. The name of the response message is again specified by convention and is identical to the request name except with .response at the end.

$MQL("rails:show.articles.response", function(message) {
  //execute some logic, usually using message.payload
});
mq/rails.txt · Last modified: 2009/02/11 12:27 by jstahl