visit
👉 Real-Time Notification System with Sidekiq, Redis and Devise in Rails 6
Hotwire can be understood as an umbrella/approach. Inside this thing, you will find 2 little boys, Turbo, that replaces TurboLinks, and Stimulus, which is, in their : A modest JavaScript framework for the HTML you already have, or the thing that we're going to use when we need to listen to some button clicks :p
I'm going to use a clean project, you can skip this if you want. Therefore:
$ rails new notifications_with_hotwire -d=postgresql -T
???
-d=postgresql
specify the DB that we're going to use. By default, Rails uses sqlite3.
-T
skips test files.
To make it more real, let's include and with their icons library, :
$ bundle add devise tailwindcss-rails heroicon
Now we need to set up those gems...
For tailwind and heroicons, just run $ rails tailwindcss:install && rails g heroicon:install
For Devise, run $ rails g devise:install && rails g devise User
and follow their notes.
I'm also going to create a resource that will dispatch notifications later on: $ rails g scaffold Post title body:rich_text user:references
. Don't forget to assign user on create,
before_action :authenticate_user!, except: [ :index, :show ]
def create
@post = current_user.posts.new(post_params)
....
has_many :posts
$ rails g model Notification item:references{polymorphic} user:references viewed:boolean
t.boolean :viewed, null: false, default: false
<%= link_to notifications_path, class: "bg-gray-800 p-1 rounded-full text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white" do %>
<span class="sr-only">View notifications</span>
<%= heroicon "bell", variant: :outline, options: { class: "h-6 w-6" } %>
<%= tag.div id: :notifications_count do %>
<%= render "notifications/count", count: current_user.unviewed_notifications_count %>
<% end %>
<% end %>
You may notice that unviewed_notifications_count doesn't exist on our User model, so let's define that as a class method on user.rb
:
has_many :notifications
def unviewed_notifications_count
self.notifications.unviewed.count
end
And the app/views/notifications/_count
partial is needed for Turbo. Going to explain this below.
<%= tag.div id: :notifications_count do %>
<% if count > 0 %>
<span class="h-6 w-6 flex items-center justify-center bg-red-500 rounded-md text-sm">
<%= count > 9 ? "+9" : count %>
</span>
<% end %>
<% end %>
Here's the thing about the whole article (notification.rb
). Turbo actions. Let's take the first callback to explain what's going on.
broadcast: now turbo handles the stuff we built on the . TD;TR: ApplicationCable to establish the connection between the user and the server, create and send jobs to the background to be performed async with Redis.
prepend/replace/remove: simple JS actions
to: the target (AKA ). In this case, I'm establishing a connection with the specific signed user.
target: simple HTML (or ) element used to perform the JS action.
scope :unviewed, ->{ where(viewed: false) }
default_scope { latest }
after_create_commit do
broadcast_prepend_to "broadcast_to_user_#{self.user_id}",
target: :notifications
end
after_update_commit do
broadcast_replace_to "broadcast_to_user_#{self.user_id}",
target: self
end
after_destroy_commit do
broadcast_remove_to "broadcast_to_user_#{self.user_id}",
target: :notifications
end
after_commit do
broadcast_replace_to "broadcast_to_user_#{self.user_id}",
target: "notifications_count",
partial: "notifications/count",
locals: { count: self.user.unviewed_notifications_count }
end
end
Yup, the latest scope didn't exist either, I like to define that on application_record.rb
.
scope :latest, ->{ order("created_at DESC") }
In application.html.erb
, create the stream that allows the previous snippet "talk" with the signed user (without this, everyone's getting everyone's notifications 😛)
<% if user_signed_in? %>
<%= turbo_stream_from dom_id(current_user, :broadcast_to) %>
<% end %>
Now let's put notifications-related stuff on concerns/notificable.rb
.
included will append where we import this concern. It sets a relationship and has a simple callback that runs once we create a new resource (post in this case) object.
module Notificable
extend ActiveSupport::Concern
included do
has_many :notifications, as: :item, dependent: :destroy
after_create_commit :send_notifications_to_users
end
def send_notifications_to_users
if self.respond_to? :user_ids
self.user_ids&.each do |user_id|
Notification.create user_id: user_id, item: self
end
end
end
end
So then, when we want to create notifications for a new resource, we just need to include that concern on our resource model, in this case post.rb
include Notificable
def user_ids
User.where.not(id: self.user_id).ids
end
Finally, to show the notifications on our application UI, let's respond to /notifications path on notifications_controller.rb
class NotificationsController < ApplicationController
before_action :authenticate_user!
def index
@notifications = current_user.notifications
@notifications.update(viewed: true)
end
end
Don't forget to notify the controller about this new path on config/routes.rb
resources :notifications, only: [ :index ]
And there you go. The notifications index :)
views/notifications/index.html.erb
<div class="space-y-4">
<p class="text-lg leading-6 font-medium text-gray-900 flex justify-between">
Notifications
</p>
<ul class="border-t border-b border-gray-200 divide-y divide-gray-200" id="notifications">
<%= render @notifications %>
</ul>
</div>
The previous render @notifications
will look for the views/notifications/_notification.html.erb
partial.
<li class="py-4" id="<%= dom_id(notification) %>">
<%= render "notifications/#{notification.item_type.downcase}", notification: notification %>
</li>
That previous render
will look for views/notifications/_post.html.erb
(in this case). If you create notifications for, let's say, likes, then you need to create a new partial, called _like
. And customize the notification for that new resource.
<p class="text-gray-900">
<%= notification.item.user.email %> just posted:
<%= link_to notification.item.title, notification.item, class: "underline font-medium" %>
</p>
Also published
Lead Photo by on