Pune Ruby Meetup - November 2015
description
Transcript of Pune Ruby Meetup - November 2015
![Page 2: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/2.jpg)
TCP IPBasic communication protocol
of the Internet.
1. HTTP 1.1 ?2. Server Sent Events ?3. WebSockets ?
![Page 3: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/3.jpg)
Options we have
Long Polling Server Sent Events Web Sockets
● Uni - directional● no multimedia
support● HTTP dependant● Native
implementation● 3-5 bytes● Eg - ActionController-
live
● Bi - directional● multimedia support● HTTP compatible but
not dependant● Native
implementation● 3-5 bytes● Eg - Action Cable
● Uni - directional● multimedia support.● HTTP dependant● Native and non-native
implementations● 663 bytes● Eg - Comet
![Page 4: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/4.jpg)
Network
1. Dude, i’m a web socket, make way for me!
2. Yes, Sure.
3. Yo communicate Yo
4. I’m done, Cya.
![Page 5: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/5.jpg)
● websocket-ruby - abstraction layer over the WebSocket API,
handshakes, frame handling, etc.
● Redis - A pub sub mechanism
● Events, Threads, Fibers, Forks - way to handle concurrent
requests
Implementation in Ruby
![Page 6: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/6.jpg)
Problems faced !
● Blocking IO● Green vs Native threads● GIL !● Server Request caching
![Page 7: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/7.jpg)
Actor Pattern Reactor Pattern
![Page 8: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/8.jpg)
Actor Pattern
● Celluloid (previously used in Sidekiq)
● Erlang, Elixir, Scala, etc
Reactor Pattern
● Event Machine (used in Thin)● Node JS
● Each actor can be a thread or a Fiber which is spawned each time.
● A Fiber is thread which run in the user space and are faster with a lower memory space.
● Fiber’s execution can be controlled by user, while thread’s cannot.
● Continuously running thread called as Event loop, blocks all resources.
● Demultiplexer sends the resource to the Dispatcher, when it is possible to start a synchronous operation on a resource without blocking.
![Page 9: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/9.jpg)
Faye (event machine + em-http-request)
● Based on Reactor Pattern● One of the best options for implementation of websockets in rails.● based on the bayeux protocol - channel management, subscription based
messaging, multiple decoupled conversations on top of a single HTTP connection.
● Runs on a separate thread (Event machine).● Provides easy client side integration.● Light coupled modules, support for extensions.● Awesome fallback mechanisms if the browser, network, server doesn’t support
websockets.● Websockets -> Server Sent Events -> XHR long polling -> CORS long polling ->
JSONP.
![Page 10: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/10.jpg)
● It provides a module for Rails controllers and a wrapper method to hijack the rack connection. Then it wraps the ruby gem websocket to handle WebSocket handshakes and frames.
● Lets you use websockets from rack and rails 4+ by using Rack's new hijack interface to access the underlying socket connection.
● No reactor, no eventmachine.● Instead, it leverages Rails 4's new full-stack concurrency support. This means you must use a concurrent
server.● Use ruby Threads inside your Rails app to hold websocket connections open without bogging down our
server.
Rack Hijack (Rack 1.5+) “Hey Rack, give me your sockets“.Gives access the underlying socket of a Rack connection in order to bidirectionally communicate with the client.
TubeSock - (Rack Hijack, native Ruby threads)
![Page 11: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/11.jpg)
Action Cable
Now what’s this new thing ?
● Faye-Websocket - websocket driver + event machine
● celluloid - efficient thread management in ruby
● em-hiredis - EM based wrapper over redis for better performance
● redis - Pub Sub service● puma - multi threaded server
Link to Git Repo
Components -
![Page 12: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/12.jpg)
Future
● Truely concurrent MRI ruby
● JRuby, Rubinius
● WebRTC ● HTTP 2.0
![Page 13: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/13.jpg)
Any questions ?
Thank you
![Page 14: Pune Ruby Meetup - November 2015](https://reader035.fdocuments.us/reader035/viewer/2022081808/5695d4871a28ab9b02a1c1a0/html5/thumbnails/14.jpg)
● http://confreaks.tv/videos/rubyconfindia2014-let-s-get-real-time-server-sent-events-websockets-and-webrtc-for-the-soul
● http://merbist.com/2011/02/22/concurrency-in-ruby-explained/● http://javieracero.com/blog/starting-with-eventmachine-i● http://old.blog.phusion.nl/2013/01/23/the-new-rack-socket-hijacking-api/● http://c7.se/intro-to-celluloid/● http://www.csinaction.com/2014/10/10/multithreading-in-the-mri-ruby-interpreter/● http://railscasts.com/episodes/260-messaging-with-faye● http://railscasts.com/episodes/367-celluloid● http://tpierrain.blogspot.in/2013/09/some-web-mechanical-sympathy-lets.html● https://blogs.oracle.com/slc/entry/introduction_to_bayeux_protocol● https://www.websocket.org/● Books - High performance browser networking
References