Skip to content

Ruby on Rails Integration

Ben Langfeld edited this page Apr 11, 2012 · 4 revisions

DEPRECATION NOTICE: This is old documentation relevant to Adhearsion 1.x and will soon be removed. See the main documentation for up-to-date info.

Ruby, the language with which Adhearsion is written, happens to have an excellent web development framework available called Ruby on Rails. Because writing a web interface for a telephony system is so highly desired, Ruby on Rails integration has obvious benefits.

Adhearsion's Rails integration is very easy to use. When you create a new application, take a look at the config/startup.rb file. It should look something like this:

config.enable_rails :path => 'gui', :env => :development

Uncomment the line to enable the integration. That's it!

Adhearsion and Rails will run as separate processes. All of your ActiveRecord models, including any Rails plugins you may have installed for them, are now available to use in your Adhearsion app. No part of your Adhearsion application will be available in your Rails application, however.

The two arguments you specify instruct Adhearsion where to find the folder containing your Rails app and what Rails environment to use. If you don't want your Rails application to be a subfolder of your Adhearsion application, you may create a symlink which points to the Rails folder within the Adhearsion application or you can provide an absolute path.

##Merging YAML configuration as well##

When using a component, you may want to merge the component's YAML configuration with that of Rails, so you have all of your configuration in one place. To do this, simply add something like this to your component's initialization:

#What to do when Adhearsion starts
initialization do
  yaml_file = File.join RAILS_ROOT, "config", "application.yml"
  if File.exist? yaml_file
   ::APP_CONFIG = YAML.load_file yaml_file
  else
    abort "#{yaml_file} does not exist!"
  end
end

Then just start adding your configuration items to RAILS_ROOT/config/application.yml. It is recommended to name space them, something like this:

adhearsion:
     timeout: 3
     trunk: grid

Then, you can start using that configuration in your component as follows:

::APP_CONFIG['adhearsion]['timeout']

##Asynchronous communication##

Adhearsion can ideally asynchronously communicate with Rails in one of two ways:

  • Through the database
  • Through message queue (MQ) middleware

There are other ways Adhearsion and Rails could communicate (for example, using the filesystem) but those two ways are recommended. The database and MQ options are desirable because they will usually queue messages if the Rails or Adhearsion processes happen to die.

If you're looking for open-source message queue middleware servers, here are a few:

You'll probably want to write custom Adhearsion components using Ruby client libraries. A Stomp client library can be found here and an AMQP client can be found here. These will soon have first-class components you can use in your applications.

##Synchronous communication##

If you want truly synchronous communication between your Rails application and Adhearsion, it's best to create a new component and add methods to the methods_for(:rpc) scope. You can then execute those methods synchronously through any RPC component you can find. Adhearsion comes with built-in support for DRb, though you could also use the restful_rpc method that comes with Adhearsion.

Also, some of the other MQ protocols support synchronous communication, notably Stomp and AMQP. Presently there are no components which streamline this, but the Adhearsion community would be more than happy to accept them!