Ajax pushing in rails 3

This a tip on real time ajax pushing using faye and private pub in rails 3

Gems used:

   gem “private_pub”
      gem ‘thin’
      gem ‘faye’

Step 1 : generate a new rails app using rails new App_name

Step 2: install the above the mentioned gem in your gem file and run the bundle command.

This a tip on real time ajax pushing using faye and private pub in rails 3

Gems used:

gem “private_pub” gem ‘thin’

gem ‘faye’

Step 1 : generate a new rails app using rails new App_name

Step 2: install the above the mentioned gem in your gem file and run the bundle command.

Step 3:  In the config -> insilizers file create a file “launch_faye.rb” so the You can run the faye pushing server with the rails server. In this file write the below code. Thread.new do if defined?(Rails::Server) system(‘bundle exec rackup private_pub.ru -s thin -E production’) end

end

Make sure it must run in production mode because faye will never run under development mode.

Step 4: In the preject folder after installing the gem there will b a new file called private_pub.ru. If it does not create automatically create it manually. Here the main problem is default configuration code will not run so to do this modify the file with the below mention code.

# Run with: rackup private_pub.ru -s thin -E production require “bundler/setup” require “yaml” require “faye”

require “private_pub”

Faye::WebSocket.load_adapter(‘thin’)

PrivatePub.load_config(File.expand_path(“../config/private_pub.yml”, __FILE__), ENV[“RAILS_ENV”] || “development”)
run PrivatePub.faye_app

Modify the code if it is running on PRODUCTION. then replace the development  with production.

Step 5: Now all the configuration is done. Now the main work is in view and controller and coffee  script.

Step 6: In the controller where u want to push the data please write
PrivatePub.publish_to(“/messages/new”, message: @message.id)

Step 7: In the View file where you want to push the data please write.

Step 8: Create a new js.coffee file with the let say messages.js.coffee. and include that file in  application.js
//= require “messages”

Step 9:  Write some coffee script in the file PrivatePub.subscribe “/messages/new”, (data, channel) ->

alert( data )

Step 10: This is all about the ajax pushing.

Main challenge : Faye must run after the whole page is loaded. there should not be any partial inclution after the faye is running. For IE it will create problem else it will run nicely for all the cases.

Reference: “http://railscasts.com/episodes/316-private-pub” In the config -> insilizers file create a file “launch_faye.rb” so the You can run the faye pushing server with the rails server. In this file write the below code.
  Thread.new do
                if defined?(Rails::Server)
                    system(‘bundle exec rackup private_pub.ru -s thin -E production’)  
                end
            end

Make sure it must run in production mode because faye will never run under development mode.

Step 4: In the preject folder after installing the gem there will b a new file called private_pub.ru. If it does not create automatically create it manually. Here the main problem is default configuration code will not run so to do this modify the file with the below mention code.

# Run with: rackup private_pub.ru -s thin -E production
          require "bundler/setup"
          require "yaml"
          require "faye"
          require "private_pub"

          Faye::WebSocket.load_adapter('thin')

          PrivatePub.load_config(File.expand_path("../config/private_pub.yml", __FILE__), ENV["RAILS_ENV"] || "development")
          run PrivatePub.faye_app

Modify the code if it is running on PRODUCTION. then replace the development  with production.

Step 5: Now all the configuration is done. Now the main work is in view and controller and coffee  script.

Step 6: In the controller where u want to push the data please write
  PrivatePub.publish_to(“/messages/new”, message: @message.id)

Step 7: In the View file where you want to push the data please write.
    

Step 8: Create a new js.coffee file with the let say messages.js.coffee. and include that file in  application.js
//= require “messages”

Step 9:  Write some coffee script in the file
  PrivatePub.subscribe “/messages/new”, (data, channel) ->
               alert( data )

Step 10: This is all about the ajax pushing.

Main challenge : Faye must run after the whole page is loaded. there should not be any partial inclution after the faye is running. For IE it will create problem else it will run nicely for all the cases.

Reference: “http://railscasts.com/episodes/316-private-pub“

150 150 Burnignorance | Where Minds Meet And Sparks Fly!