Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails实时web开发

 Rails实时web开发

Realtime web Rails solutions

Avatar for Leslie Zhao

Leslie Zhao

October 15, 2016

More Decks by Leslie Zhao

Other Decks in Technology

Transcript

  1. Talk Points • Scenario • Realtime History • Main solutions

    • RubyChina’s Practices • Related Service Providers 16年10⽉月14⽇日 星期五
  2. Scenario • Realtime Notifications • Realtime Chat • Multiplayer Games,

    Collaboration • Activity Streams • Commenting System • Realtime Progress Bar, Stats 16年10⽉月14⽇日 星期五
  3. HTTP • 1996, HTTP 1.0 分布式、⽆无状态、基于TCP的请求/响应 式 • 1999, HTTP

    1.1* 默认⻓长连接之外就是缓存处理、带宽优 化和安全性等⽅方⾯面的不痛不痒的改进 • ????, HTTP 2.0 • SPDY* 16年10⽉月14⽇日 星期五
  4. Main Solutions • Ajax polling (2005, Google) • Comet(long polling,

    stream), a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications • WebSocket (2008) • SSE(Server-sent Events) • WebRTC(2010, Google) 16年10⽉月14⽇日 星期五
  5. WebSocket GET /chat HTTP/1.1 Host: xxxx.com Upgrade: websocket Connection: Upgrade

    Sec-WebSocket-Key: XXXXXX Sec-WebSocket-Protocol: XXXX Sec-WebSocket-Version: X Origin:http://xxxx.com 16年10⽉月14⽇日 星期五
  6. Review: Polling • 客户:我的快递到了没呀?(request) • 客服:你好,你是?没有(response) • 客户:我的快递到了没呀? • 客服:你好,你是?没有,真没有

    • 客户:我的快递到了没呀? • 客服:你好,你是?!@#$%&*,可能还再路上 • 客户:我的快递到了没呀? • 客服:你好,你是?有了会给你送来的 • 客户:我的快递到了没呀? • 客服:你好,你是?没.....没.....没有啊....啊啊啊啊 16年10⽉月14⽇日 星期五
  7. Review: WebSocket • 客户:啦啦啦,我要建⽴立WebSocket协 议,需要的服务:XXX 协议版本: x(HTTP Request) • 客服:ok,确认,已升级为WebSocket协

    议(HTTP Protocols Switched) • 客户:⿇麻烦你有信息的时候推送给我 噢。。 16年10⽉月14⽇日 星期五
  8. Faye • Faye is a set of tools for simple

    publish- subscribe messaging between web clients. It ships with easy-to-use message routing servers for Node.js and Rack applications, and clients that can be used on the server and in the browser. 16年10⽉月14⽇日 星期五
  9. Faye Based on the Bayeux* protocol The Adapter, as implemented

    by the NodeAdapter and RackAdapter classes, exposes the Server’s interface over HTTP. It is responsible for serializing and deserialzing messages as JSON and accepting connections over various flavours of HTTP transport: Persistent connections using WebSocket Long-polling via HTTP POST Cross Origin Resource Sharing Callback-polling via JSON-P 16年10⽉月14⽇日 星期五
  10. MessageBus MessageBus was extracted out of Discourse and is used

    in thousands of production Discourse sites at scale. 16年10⽉月14⽇日 星期五
  11. MessageBus A reliable, robust messaging bus for Ruby processes and

    web clients. MessageBus implements a Server to Server channel based protocol and Server to Web Client protocol (using polling, long-polling or long-polling + streaming) Long-polling is implemented using Rack Hijack and Thin::Async, all common Ruby web server can run MessageBus (Thin, Puma, Unicorn) and handle a large amount of concurrent connections that wait on messages. MessageBus is implemented as Rack middleware and can be used by and Rails / Sinatra or pure Rack application. 16年10⽉月14⽇日 星期五
  12. Action Cable Action Cable seamlessly integrates WebSockets with the rest

    of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable. 16年10⽉月14⽇日 星期五
  13. RubyChina的演进 • 2012-08-29 Faye realtime push notifications • 2015-04-22 MessageBus

    代替 Faye • 2016-02-02 ActionCable 代替 MessageBus 16年10⽉月14⽇日 星期五
  14. nginx-push-stream-module Fast, horizontally scalable, multiprocess pub/sub queuing server and proxy

    for HTTP, long- polling, Websocket and EventSource (SSE), powered by Nginx. pure stream http push technology for your Nginx setup. Comet made easy and really scalable. Supports EventSource, WebSocket, Long Polling, and Forever Iframe. 16年10⽉月14⽇日 星期五
  15. See Also • http://tenderlovemaking.com/2012/07/30/is-it-live.html • http://wulfric.me/2016/09/active-job-action-cable-in-rails/ • https://meta.discourse.org/t/how-discourse-stays-online-message-bus-faye-long-polling/ 3238/21 •

    http://www.cnblogs.com/helloim/p/5663660.html • http://jargon.io/joeyschoblaska/rails-5-chess-with-action-cable-websockets • https://blog.heroku.com/ real_time_rails_implementing_websockets_in_rails_5_with_action_cable • https://www.twilio.com/blog/2016/06/real-time-rails-4-using-server-sent-events-with- authy-onetouch.html • https://www.startuprocket.com/articles/rails5-actioncable-redux-and-react-walking- through-an-example-chat-application • https://blog.pusher.com/how-to-add-message-history-to-your-pusher-apps/ • http://www.thegreatcodeadventure.com/rails-5-action-cable-with-multiple-chatroom- subscriptions/ 16年10⽉月14⽇日 星期五
  16. • https://hectorperezarenas.com/2015/12/26/rails-5-tutorial-how-to-create-a-chat-with- action-cable/ • https://www.sitepoint.com/action-cable-and-websockets-an-in-depth-tutorial/ • https://www.onehub.com/blog/2016/02/18/event-notifications-in-rails-5-with- actioncable/ • https://blog.ably.io/rails-5-actioncable-the-good-and-bad-parts-1b56c3b31404#.4iigbo3jg

    • https://www.smashingmagazine.com/2012/05/building-real-time-commenting-system/ • http://www.ikantam.com/blog/chat-rooms-use-websockets-rails • https://www.sitepoint.com/mini-chat-rails-server-sent-events/ • http://www.thegreatcodeadventure.com/deploying-action-cable-with-a-single-server/ • http://www.thegreatcodeadventure.com/deploying-action-cable-to-heroku/ • http://www.thegreatcodeadventure.com/rails-5-preview-action-cable/ See Also 16年10⽉月14⽇日 星期五