Rails: send headers before body
===

I'm porting "notifications" type functionality of a PHP application to a Rails application.

In our JS, we made a request to the API. The API sent headers, then periodically checked the DB for new notifications. When it found some, it sent it out to the requesting client, which just ran the call recusively. The PHP code to accomplish this is:

    header("Cache-Control: no-cache, must-revalidate");
    date_default_timezone_set('GMT');
    header("Expires: " . date("D, d M Y H:i:s e", time() + 3600));
    flush();

    $time = 0;

    while (!$this->newNotifications($user_id)) {
       sleep(1);

       if (++$time == 60) {
          echo json_encode(array());
          flush();
          die();
       }
    }

    $new_notifications = $this->getUndeliveredNotifications($user_id);
    $new_notifications_ids = array();

    foreach ($new_notifications as $notification)
       $new_notifications_ids[] = $notification['notification_id'];

    $this->markAsDelivered($new_notifications_ids);

    echo json_encode($new_notifications);
    flush();
    die();


My rails application is using [RABL][1] to template the JSON. Is there a way to imitate the above PHP code in Rails?


  [1]: https://github.com/nesquena/rabl/