Pune Ruby Meetup - November 2015 PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Pune Ruby Meetup - November 2015


1
Websockets in Rails
  • by Vineet Ahirkar - _at_vinzee93

2
TCP IP
  1. HTTP 1.1 ?
  2. Server Sent Events ?
  3. WebSockets ?
  • Basic communication protocol
  • of the Internet.

3
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

4
Network
Dude, im a web socket, make way for me!

Yes, Sure.

Yo communicate Yo

Im done, Cya.

5
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
6
Problems faced !

Blocking IO
Green vs Native threads
GIL !
Server Request caching
7
Actor Pattern
Reactor Pattern
8
Actor Pattern
Reactor Pattern
  • 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.
  • Fibers execution can be controlled by user,
    while threads 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.
  • Celluloid (previously used in Sidekiq)
  • Erlang, Elixir, Scala, etc
  • Event Machine (used in Thin)
  • Node JS

9
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 doesnt support websockets.
  • Websockets -gt Server Sent Events -gt XHR long
    polling -gt CORS long polling -gt JSONP.

10
TubeSock - (Rack Hijack, native Ruby threads)
  • 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.

11
Action Cable
Components -
  • 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
  • Now whats this new thing ?

Link to Git Repo
12
Future
  • Truely concurrent MRI ruby
  • JRuby, Rubinius
  • WebRTC
  • HTTP 2.0

13
Thank you
  • Any questions ?

14
References
  • http//confreaks.tv/videos/rubyconfindia2014-let-s
    -get-real-time-server-sent-events-websockets-and-w
    ebrtc-for-the-soul
  • http//merbist.com/2011/02/22/concurrency-in-ruby-
    explained/
  • http//javieracero.com/blog/starting-with-eventmac
    hine-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/multithreadin
    g-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-mech
    anical-sympathy-lets.html
  • https//blogs.oracle.com/slc/entry/introduction_to
    _bayeux_protocol
  • https//www.websocket.org/
  • Books - High performance browser networking
Write a Comment
User Comments (0)
About PowerShow.com