Back to Home

Faye as a way to not screw up your server

ruby on rails · faye · comet

Faye as a way to not screw up your server

    Hello, habralyudi. Today I want to talk about how in one of our projects we reduced the number of server requests by a couple of orders due to the use of Comet technology .

    The essence of the problem: the web application for the taxi service, the dispatcher work remotely (from home, from the office, etc.). Before each - a list of orders. The status of orders can change at any time - the driver took the order, the driver refused, the client refused, another dispatcher opened a new order. It is very important that the dispatcher sees the picture in almost real time, because at rush hour calls go one after another without interruption. At first, this was done through an AJAX request called periodically (setTimeout), which received a rendered list of current requests from the server and replaced the contents of the div. For 1-2 developers, and a couple of testers, this seemed like a normal idea - 1-2 requests per second were not very annoying. According to the results of a compromise between relevance and reduced load on the server, we decided that we would pull the server every 3 seconds.

    However, real life quickly put everything in its place. Firstly, in reality, the list of orders is not 5-10, but 30-40 simultaneously executed orders. Secondly, there are about a dozen dispatchers. The load on the server confidently crept up. We decided not to wait until they put a server on us with increasing popularity and redid the list update based on Comet technology, and more specifically, based on the useful faye gem .

    The idea is based on the Long Polling principle - when a page loads a javascript, it makes a hidden AJAX request to the message server, and the server doesn’t give a byte so far. Then either the request falls off by timeout - then the javascript repeats the request, or the normal javascript that will be executed on the client side comes back.

    Using the gem is quite simple:
    1. Install the faye gem (add it to the Gemfile and do bundle install)

    2. Add it to config / application.rb This is necessary for the message sending helper to work - probably, then integrate it into the gem, but for now you need to enter it with your hands. 3. Create a new file with text. This sets the line by which the message server will distinguish one application from another, if there are several of them. Instead of “anything,” you can write anything, only to not get confused later. 4. Create a helper for sending messages (for example, in application_helper.rb) This helper is then easy to use somehow in any view: 5. Create a file in the root of the project, for example faye.ru
    require "net/http"






    FAYE_TOKEN = "anything"




    def broadcast(channel, &block)
    message = {:channel => channel, :data => capture(&block), :ext => {:auth_token => FAYE_TOKEN}}
    uri = URI.parse("http://moesuperprilozhenie.ru:9292/faye")
    Net::HTTP.post_form(uri, :message => message.to_json)
    end



    - broadcast "/orders/update" do
    $("#orders_list").append("#{escape_javascript render(@orders) }");



    require 'faye'
    require File.expand_path('../config/initializers/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_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
    faye_server.add_extension(ServerAuth.new)
    run faye_server


    This is an instruction for launching the Faye Rack server, which will actually send updates to clients.

    6. Starting the message server: Here we start the message server in accordance with the file faye.ru, based on the thin Web server and in production mode. There is a subtle point here - the -E production argument does not apply to the environment of your application, but to the mode of operation of faye and thin itself. If you specify -E development - there will be glitches, why I do not know, maybe they fixed it . 7. In javascript to load the page: 8. Add faye.js to the list of plug-in javascripts (taken directly from the message server, in our case moesuperprilozhenie.ru : 9292 / faye). An example on haml: 9. We open the newly-screwed Selektelovsky
    rackup faye.ru -s thin -E production






    $(function() {
    var faye = new Faye.Client('http://moesuperprilozhenie.ru:9292/faye');
    faye.subscribe("/orders/update", function(data) {
    eval(data);
    });
    });



    = javascript_include_tag "http://moesuperprilozhenie.ru:9292/faye.js"


    CPU load graph and rejoice.

    Hope it helps someone. I find it difficult to randomly list the methods of using Comet, various chats, pagers, notifications, as well as various monitoring come to mind.

    UPD: The code in the article is taken from Railscast . Ryan Bates respect and respect.

    Read Next