title: Faye Messaging on Heroku
body: <p>I keep getting a 503 error 30 seconds after a client sends a message through faye. After the 30 seconds the client then receives the message and it is appended to the chat but the error still occurs and the socket will eventually close. How can i modify my existing code to keep the websocket alive? And how can I get rid of the 30 second delay that heroku throws when a message is sent?</p>
  
  <p><strong>messages/add.js.erb</strong></p>
  
  <pre><code>&lt;% broadcast @path do %&gt;
    var $chat = $("#chat&lt;%= @conversation.id %&gt;");
    $chat.append("&lt;%= j render(@message) %&gt;");
    //Set the scroll bar to the bottom of the chat box
    var messageBox = document.getElementById('chat&lt;%= @conversation.id %&gt;');
    messageBox.scrollTop = messageBox.scrollHeight;
  &lt;% end %&gt;
  $("#convoId&lt;%=@conversation.id%&gt;")[0].reset();
  </code></pre>
  
  <p><strong>application_helper.rb</strong></p>
  
  <pre><code>def broadcast(channel, &amp;block)
    message = {:channel =&gt; channel, :data =&gt; capture(&amp;block), :ext =&gt; {:auth_token =&gt; FAYE_TOKEN}}
    uri = URI.parse(FAYE_END_PT)
    Net::HTTP.post_form(uri, :message =&gt; message.to_json)
  end
  </code></pre>
  
  <p><strong>application.rb</strong></p>
  
  <pre><code>config.middleware.delete Rack::Lock
  config.middleware.use FayeRails::Middleware, mount: '/faye', :timeout =&gt; 25
  </code></pre>
  
  <p><strong>faye.ru</strong></p>
  
  <pre><code>require 'faye'
  require File.expand_path('../faye_token.rb', __FILE__)
  
  class ServerAuth
    def incoming(message, callback)
      if message['channel'] !~ %r{^/meta/}
        if message['ext']['auth_token'] != FAYE_TOKEN
          message['error'] = 'Invalid authentication token'
        end
      end
      callback.call(message)
    end
  end
  Faye::WebSocket.load_adapter('thin')
  faye_server = Faye::RackAdapter.new(:mount =&gt; '/faye', :timeout =&gt; 45)
  faye_server.add_extension(ServerAuth.new)
  run faye_server
  </code></pre>
  
  <p><strong>Procfile</strong></p>
  
  <pre><code>web:    bundle exec rails server -p $PORT
  worker: bundle exec foreman start -f Procfile.workers
  </code></pre>
  
  <p><strong>Procile.workers</strong></p>
  
  <pre><code>faye_worker: rackup middlewares/faye.ru -s thin -E production
  </code></pre>
  
  <p><strong>503 Error</strong></p>
  
  <pre><code>/messages/add Failed to load resource: the server responded with a status of 503 (Service Unavailable)
  </code></pre>
  
  <p>I tried adding a worker to heroku along with a web dyno with no luck. Everything works fine on my local host when running <code>heroku local</code>. The process on the local host look like </p>
  
  <pre><code>forego  | starting web.1 on port 5000
  forego  | starting worker.1 on port 5100
  worker.1 | 20:33:18 faye_worker.1 | started with pid 16534
  </code></pre>
  
  <p>where as even with the web dyno and worker on heroku </p>
  
  <pre><code>=== web (1X): bundle exec rails server -p $PORT
  web.1: up 2015/12/28 20:08:02 (~ 1h ago)
  
  === worker (1X): bundle exec foreman start -f Procfile.workers
  worker.1: up 2015/12/28 21:18:39 (~ 40s ago)
  </code></pre>
  
  <p>A lot of this code was taken from various tutorials so hopefully if we can solve this issue it will make using Faye with Heroku easier to someone else as well. Thanks!</p>