Action Cable

Rails follows the MVC architecture and by default uses the HTTP protocol to handle the request-response cycle in a web application. However, since Rails version 5, the Action Cable feature was included to lend support for the new WebSocket protocol.

Unlike the HTTP protocol, WebSocket is an asynchronous protocol, thus enabling real-time communication between the client and the server.

In Rails 8, Action Cable is enabled by default. In earlier versions, it must be included as a gem to be able to use.

Add this to the Gemfile −

gem "action_cable"

and then,

bundle install

What is Action Cable?

Action Cable lets you seamlessly integrate the WebSockets in your Rails application. You can write performant and scalable real-time features in Ruby that has the access to the Active Record or your ORM of choice.

While a regular Rails server accepts only HTTP requests, an ActionCable server accepts WebSocket requests. The ActionCable server subscribes to channels and broadcasts from channels using Redis.

What is Action Cable?

Action Cable seamlessly integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable. It’s a full-stack offering that provides both a client-side JavaScript framework and a server-side Ruby framework. You have access to your entire domain model written with Active Record or your ORM of choice.

To mount Action Cable, you need to edit the routes.rb file of your application −

mount ActionCable.server =>'/cable'

Action Cable Terminology

Action Cable uses WebSockets instead of the HTTP request-response protocol. One needs to get familiar with the following terms:

Connections

Connections are fundamental to the client-server relationship. A single Action Cable server can handle multiple connection instances. It has one connection instance per WebSocket connection.

A new connection object is instantiated for every WebSocket accepted by the server. The connection object is a parent of all subsequent channel subscriptions, although it deals only with authentication and authorization and not any specific application logic.

Consumers

The client of a WebSocket connection is called the connection consumer. An individual user will create one consumer-connection pair per browser tab, window, or device they have open.

Consumers

Every client of a WebSocket connection is called the consumer. In Action Cable, the consumer is created by the client-side JavaScript framework.

Channels

A channel is similar to what a consumer is in a MVC framework. Each channel encapsulates a logical unit of work. A consumer can subscribe to multiple channels.

For example, you could have a ChatChannel and an AppearancesChannel, and a consumer could be subscribed to either or both of these channels. A consumer should be subscribed to one channel.

You can generate new channels where WebSocket features live using the generate channel command.

rails generate channel Chatroom

It creates the ChatroomChannel class which inherits class. You can override the following methods from the parent −

  • subscribed − Called when a client successfully subscribes to the channel.
  • unsubscribed − Called when the client disconnects or unsubscribes.
  • receive(data) − Called when the client sends a message via the WebSocket.
  • stream_from(stream_name) − Starts streaming messages from a named broadcasting channel to the client.
  • transmit(data) − Sends data directly to the client without a broadcast.

Subscribers

When the consumer is subscribed to a channel, he becomes a subscriber. The connection between the subscriber and the channel is called a subscription.

A consumer can subscribe to a given channel any number of times. For example, a consumer could subscribe to multiple chat rooms at the same time.

Publishers

In Action Cable, your application code (controllers, models, jobs, etc.) acts as the publisher, sending data or events to a specific channel.

Pub/Sub Pattern

As publishers send messages to a channel, subscribers (clients) listen for messages on that channel. This is called the publish/subscribe pattern.

Action Cable uses the (Pub/Sub) pattern which refers to a message queue paradigm in which the senders (publishers) send data to an abstract class of recipients (subscribers), without specifying individual recipients. Action Cable uses this approach to communicate between the server and many clients.

Broadcastings

broadcasting is a pub/sub link where anything transmitted by the broadcaster is sent directly to the channel subscribers who are streaming that named broadcasting. Each channel can be streaming zero or more broadcastings.

With these basics of Action Cable features understood, let us build a simple real-time chat application using Action Cable and WebSocket.

Comments

Leave a Reply

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