Pune Ruby Meetup - November 2015

14
Websockets in Rails by Vineet Ahirkar - @vinzee93

description

After a successful Ruby Meetup in June, Amura hosted PunesRuby's Meetup for the month of November '15. We had our very own team members give some really awesome talks about Ruby and technology in general.RequireJS - An Introduction - Vrushali WaykoleWebSockets in Rails - Vineet AhirkarInformation about Ruby Association Grant - Sameer Deshmukh

Transcript of Pune Ruby Meetup - November 2015

Page 1: Pune Ruby Meetup - November 2015

Websockets in Rails

by Vineet Ahirkar - @vinzee93

Page 2: Pune Ruby Meetup - November 2015

TCP IPBasic communication protocol

of the Internet.

1. HTTP 1.1 ?2. Server Sent Events ?3. WebSockets ?

Page 3: Pune Ruby Meetup - November 2015

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

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

● 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

Problems faced !

● Blocking IO● Green vs Native threads● GIL !● Server Request caching

Page 7: Pune Ruby Meetup - November 2015

Actor Pattern Reactor Pattern

Page 8: Pune Ruby Meetup - November 2015

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

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

● 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

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

Future

● Truely concurrent MRI ruby

● JRuby, Rubinius

● WebRTC ● HTTP 2.0

Page 13: Pune Ruby Meetup - November 2015

Any questions ?

Thank you

Page 14: Pune Ruby Meetup - November 2015

● 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