-1

ActionController::Live in Rails 4 !

1.Definition

Rails 4.0 will allow you to stream arbitrary data at arbitrary intervals with Live Streaming.

2.Compare rails 4 and rails 3

Rails 3 will buffer the data until the response is completed, then send everything at the same time.

Rails 4 but this time the data will be streamed to the client as every time we call the write method.

The ability to deliver whatever data you want, whenever you want.

3.There are a few caveats with this use.

You cannot write headers after the response has been committed.

You must call close on your stream when you’re finished, otherwise the socket may be left open forever.

Your actions are executed in a separate thread than the main thread. Make sure your actions are thread safe, and this shouldn’t be a problem (don’t share state across threads, etc).

  1. About Server

By default, script/rails server uses WEBrick. The Rack adapter for WEBrick buffers all output in a way we cannot bypass, so example use puma.

5.Reference

http://edgeapi.rubyonrails.org/classes/ActionController/Live.html

http://tenderlovemaking.com/2012/07/30/is-it-live.html

http://blog.remarkablelabs.com/2012/12/live-streaming-rails-4-countdown-to-2013

http://railscasts.com/episodes/401-actioncontroller-live

6.Demo

http://www.fshare.vn/file/SDYCFXRBU1/

password download : framgia


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí