Creating Realtime Notification in Rails 6

Tram Ho


Action cable has Rails integrated WebSocket to support realtime written in Ruby to allow two-way communication between client and server. Data is transmitted via HTTP (Ajax) protocol.

It allows writing real-time features. It provides a full range of features to support the link between client-side javascrip framework and sever-side Rail framework.


Step 1

  • Create the Notification table

Establish a relationship model

Step 2

  • Create view

  • Partial View when new Notification is available

  • Partial View to count the number of Notifcation

Step 3

  • ActionCable will allow you to open the chanel and keep the chanel connected to the server without having to refresh the page. First we will initialize the project chanel with syntax

  • Rails will automatically create two more files for us, app/channels/notifications_channel.rb and app/javascript/channels/notifications_channel.js

Step 4

  • Set the connetion on the server side

Step 5

  • Set up channels on server side as follows:

Step 6

  • Establish a connection on the client side

Step 7

  • Set up Subscribers on the client side

Step 8

  • Create a job to perform the response for the client, and the broadcast will call the channel.

Step 9

  • Finally, we will use Active Record’s Callbacks to call Jobs to perform the response to the client.


So we have completed the notification creation in the application, I am a new rails learner so the article has many shortcomings, hope everyone can comment to be able to improve.


Share the news now

Source : Viblo