Pune Ruby Meetup - November 2015
-
Upload
amura-marketing-technologies-pvt-ltd -
Category
Technology
-
view
285 -
download
0
Transcript of Pune Ruby Meetup - November 2015
Websockets in Railsby Vineet Ahirkar - @vinzee93
TCP IPBasic communication
protocol of the Internet.
1.HTTP 1.1 ?
2.Server Sent Events ?
3.WebSockets ?
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
Network
1. Dude, i’m a web socket, make way for me!
2. Yes, Sure.
3. Yo communicate Yo
4. I’m done, Cya.
● 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
Problems faced !
● Blocking IO● Green vs Native
threads● GIL !● Server Request
caching
Actor PatternReactor Pattern
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.
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.
● 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)
Action CableNow 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 -
Future● Truely concurrent MRI
ruby● JRuby, Rubinius
● WebRTC ● HTTP 2.0
Any questions ?
Thank you
● 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-interpr
eter/● 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.htm
l● https://blogs.oracle.com/slc/entry/introduction_to_bayeux_protocol● https://www.websocket.org/● Books - High performance browser networking
References