ActionCable and WebSockets – Part 1(The Basics)

One of the best thing about Rails is the ease with which it allows you to develop web apps quickly by providing some sensible conventions. And with Rails 5, it allows you to make real-time web apps in a breeze Introducing ActionCable.. a real time framework for communication over websockets. But before we proceed any further, let’s spend some time discussing how we got to the action cable.. or particularly, the web sockets. When the web started becoming more dynamic with ajax and js advances, we, as developers, started finding ways to make our applications more real-time. POLLING One of the earliest solutions that came up was…

ActionCable and WebSockets – Part 2(The Implementation)

You can visit the Part 1 here. THE RAILS WAY… When you create a new rails 5 application, rails generates some files for you: For any implementation of a websocket connection, we need both the client and the server parts of the code. CLIENT SIDE For the client side Rails provides app/assets/javascripts/cable.js which loads action_cable js and all files in channels directory. On page load, a consumer is created and exposed via App.cable. If we would go a little bit into the client side code for action_cable, we would find that rails does all the heavy loading like instantiating subscriptions and connections…monitoring connections etc. pretty cool.. right ?…

WebSockets with Cramp

Rails itself is not good at asynchronous event handling. And because of this it is bad at push techniques and also not a very good candidate for real time web applications. This problem exists in Rails because we can’t keep a bidirectional socket open between our client and a Rails application. There are many solution that solve this problem. Node.js and Socket.io are some javascript based solutions. Whereas Faye, Goliath, async_sinatra and Cramp are some Ruby based solutions. Cramp is an asynchronous framework running inside EventMachine loop. It means that while Cramp is communicating with a client over a connection, it can handle another connection with some…