Action Cable in Rails

Tram Ho

Hi everyone, today we will learn about Action Cable in Rails. ?

Introduction

Action Cable integrates Websockets with the Rails app. It allows Rails to build realtime features.

full-stack offering : provides client-side JavaScript framework, and Ruby server-side framework.

Terminology

An Action Cable server can handle multiple incoming connections. Each Websocket connection will have a corresponding connection instance.

A user can have multiple Websocket connections to the app if he or she has multiple tabs open on browsers or devices.

Clients with websocket connections are called consumer . Each consumer can subscribe to multiple cable channel channels. channel is where the logic is, as the controller is in the MVC model.

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


Server-Side Components

1. Connections

Connection establishes a relationship between the client and the server, with each Websocket connecting to the server, a connection object is instantiated. This object becomes the father of all channel subscriptions created later.

Connection is an instance of ApplicationCable::Connection . In this class, we will delegate and make a connection if the user is specified.

1.1 Connection setup

identified_by the connection to make it easier to find the connection later.

cookie automatically sent to the connection instance when there is a new connection , and we use it to set the value for the current_user . By identifying each connection with current_user , we can retrieve that user’s connection .

2. Channels

channel is where the logic is, as the controller is in the MVC model. channel created by the ApplicationCable::Channel class.

2.1 Parent channel setup

We can then create our own channel :

2.2 Subscriptions

When a consumer subscribes to a channel , becomes a subscribers , the connection between the two is called the subscription

Client-Size Components

1. Connections

The client side also needs a connection object to establish a connection. It is created by default in Rails :

1.1 Connect consumer

By default, consumer will connect to /cable to the server. connection will not be established until you create a subscription .

1.2 Subscribers

A consumer becomes a subscriber by creating a subscription to a channel :

Client-Server Interations

1. Streams

Streams provide a mechanism for channel to route the content they want to send ( broadcast ) to subscribers

If we have a stream linked to the model , broadcasting can be created from the model and channel :

Then we can broadcast to this channel:

2. Broadcasting

broadcasting is a pub/sub (published, subscriber), where everything passed by the publisher is routed directly to the subscribers are streaming with this broadcasting ‘s name. Each channel can streaming one or more broadcasting .

broadcast can be called elsewhere in Rails :

3. Subscriptions

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

These messages are routed to these channel subscriptions based on identifiers sent by cable consumer .

4. Passing parameter to channels

We can pass params from the client to the server when creating new subsciptions :

The first parameter passed to subscriptions.create becomes the params hash in the cable channel :

Examples

Receiving New Web Notification

Create notifications channel :

Client side:

Broadcast:


Share the news now

Source : Viblo