Ruby on Rails and Blogging

JenTutorials, Web Development0 Comments


I was listening to a Ruby Rogues podcast this morning that was aimed at new developers.  The episode came about as a result of this awesome Scope Wars post by Malinna Leach. They were discussing the importance of developers blogging and how it can help not only get a job, but also increase your understanding about the concepts that you are learning.  Blogging was a requirement for the Flatiron program as well, so I definitely see the advantages.

When I look at the analytics for my site, I can definitely tell when a potential employer has been looking at my blog.  Just yesterday, I had an interview where the person had read over several of my previous blog posts and most of the questions that he asked were about topics and things that I had mentioned here.

This week was a very busy week preparing for interviews, but I did manage to learn about something really interesting that is a feature of Rails 5 – Action Cable.  Action Cable integrates WebSockets into your Rails application.  It enables information to be updated in real-time without refreshing the page.

So the first piece that I had to dig into was exactly what WebSockets are. In a normal HTTP request, the browser requests data and the server sends the data back to the browser.  In order to update that information, the browser would need to send another request.

Enter WebSockets. WebSockets are connections that are stateful. So basically, the connection between a client and a server remains constant and connected. In this scenario. either party (the client or the server) has the ability to initiate a request or a message. The end result is direct interaction between browser and server.

Action Cable provides both a client-side JavaScript framework and a server-side Ruby framework. It allows you to have access to your full domain model written with Active Record or your ORM of choice. It uses connections as the foundation of the client-server relationship.

According to Rails Guides, “For every WebSocket accepted by the server, a connection object is instantiated. This object becomes the parent of all the channel subscriptions that are created from there on. The connection itself does not deal with any specific application logic beyond authentication and authorization. Connections are instances of ApplicationCable::Connection. In this class, you authorize the incoming connection, and proceed to establish it if the user can be identified.”

Action Cable also has channels.  Channels are similar to the controller of an MVC application.  It handles all of the logic of the connections. Consumers can subscribe to channels, initiating a subscription. Messages are then routed to the channel subscriptions based on information sent by the consumer.

On the client side, connections are handled through Javascript.  Rails provides the basic Javascript framework by default and will actually create a consumer that will connect to your server. Channels then publish content through the use of streams. Streams can be connected to a model and is then able to broadcast information.

There are some great resources out there to get started with Action Cable.  I recommend reading through the Rails Guides to further educate yourself.  There is also a great tutorial that walks you through creating a simple app using Action Cable if you would like something more hands-on.

Leave a Reply

Your email address will not be published. Required fields are marked *