开发者

AJAX and Rails: two stacks or one?

开发者 https://www.devze.com 2023-01-22 15:52 出处:网络
Most of a web application is pretty standard CRUD. I need some data to be pushed to clients live as it\'s created. Is it worthwhile to run a separate, more lightweight stack (such as Sinatra or EventM

Most of a web application is pretty standard CRUD. I need some data to be pushed to clients live as it's created. Is it worthwhile to run a separate, more lightweight stack (such as Sinatra or EventMachine) for the AJAX?

If I run one stack, I'll have Rails' overhead. This might require more Rails processes / servers, so how do I communicate between these without database queries? ZeroMQ? Finally, is Rails开发者_如何学Go suited to handle hundreds of concurrent connections?

If I run two stacks, I'll need to duplicate authorization logic.

Which methods have been successful for you?


In Rails 3 you can pretty easily hook in Rack apps using the new routing syntax and inheriting your controllers from ActionController::Metal or just defining self.call.

Have a read (or watch) of Railscast #222 and check out wycats' blog (see below for example excerpts).

You're already aware of the additional cases you'll need to handle if you go down this track, so make sure it's necessary before getting stuck into it. It's usually cheaper to drop $ on servers than on programmers - hardware and bandwidth is pretty cheap.

I personally feel like this falls under 'premature optimization' unless you app is already running and choking up with too much traffic.


A few nice examples:

# config/routes.rb
# Hook in Sinatra
root :to => HomeApp
# Write your own barebones Rack compatible code
match "/processes" => ProcessesApp
# Even specify an inline proc
match "/heartbeat", :to => proc {|env| [200, {}, ["App is running"]] }

# /lib/home_app.rb
class HomeApp < Sinatra::Base  
  get "/" do  
    "Hello from Sinatra"  
  end  
end  

# lib/processes_app.rb
class ProcessesApp  
  def self.call(env)  
    [200, {}, [`ps -axcr -o "pid,pcpu, pmem, time, comm"`]]  
  end  
end  
0

精彩评论

暂无评论...
验证码 换一张
取 消