I'm trying to create a Desktop Notification feature in my RoR application using Action Cables. My typical user will have multiple panes open at any given time which presents me with a unique obstacle. A single user may have `n` windows/panes open, meaning `n` consumers. This means that any one event will trigger `n` identical Desktop Notifications, which provides a poor user experience.
I could reject any request to connect after the first but this would mean that any future Action Cable features I build will also only be available to the first window/pane which is no good.
# app/channels/application_cable/connection.rb module ApplicationCable class Connection < ActionCable::Connection::Base identified_by :current_user def connect self.current_user = find_verified_user end protected def find_verified_user if current_user = User.find(cookies.signed[:user_id]) current_user else reject_unauthorized_connection end end end end
# app/channels/desktop_notification_channel.rb class DesktopNotificationsChannel < ApplicationCable::Channel def subscribed stream_from 'desktop_notifications' end end
# app/operations/desktop_notifications/desktop_notification.rb module DesktopNotifications class DesktopNotification def initialize end def broadcast ActionCable.server.broadcast( 'desktop_notifications', title: title, body: @body, image_url: logo_url, destinations: destinations ) end # some other helpers end end
Essentially I want something that will let me control a user's subscriptions and only display one Push Notification per user while still being able to broadcast to all of the users who are subscribed (think User-specific mutex for a subscription).
Any ideas would be greatly appreciated!