dobus
dobus

Reputation: 97

Starting Faye With Rails Automatically

I am trying to start Faye when starting my rails server. I have a faye.ru file in my app root that looks like:

require 'faye'
faye_server = Faye::RackAdapter.new(:mount => '/queue-listener', :timeout => 45)
run faye_server

And whenever I start my rails server, Faye / thin tries to open on the same port as my rails server. I can add something like:

Thread.new do  
  system("rackup faye.ru -s thin -E production")  
end  

into an initializer (found this on SO), but then thin starts on both the rails app port and then the default (9292) port. I think it fails to start on the rails port though. I am just confused about how to start up the thin / faye server on a separate port than the rails server. Any ideas?

Upvotes: 1

Views: 1295

Answers (1)

Roman
Roman

Reputation: 13058

You can do the following in development. In production I suggesting implementing it as a standalone server w/monitoring:

if Rails.env.development?
  require 'eventmachine'
  require 'rack'
  require 'thin'
  require 'faye'

  Faye.logger = Logger.new(Rails.root.join('log/faye.log'))

  Faye::WebSocket.load_adapter('thin')

  thread = Thread.new do
    EM.run {
      thin = Rack::Handler.get('thin')
      app = Faye::RackAdapter.new(mount: '/faye', timeout: 10)
      thin.run(app, :Port => 8000) do |server|
        ## Set SSL if needed:
        # server.ssl_options = {
        #   :private_key_file => 'path/to/ssl.key',
        #   :cert_chain_file  => 'path/to/ssl.crt'
        # }
        # server.ssl = true
      end
    }
  end
  at_exit { thread.exit }
end

Upvotes: 1

Related Questions