Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use action cable to broadcast notification JSON instead of HTML #111

Closed
farooqch11 opened this issue Aug 29, 2019 · 6 comments
Closed

Comments

@farooqch11
Copy link

i am integrating this gem in rails as API and facing alot of over rides!
need help to over ride the action cable method to broadcast JSON

right now its posting HTML

@simukappu
Copy link
Owner

Hi, thank you for your question and feedback.
The most simple way to customize Action Cable response is customizing notification view.
https://github.com/simukappu/activity_notification#notification-views
As you mentioned, default views are HTML format. You can customize it into JSON format representing your notification model as you like.

If you are expecting activity_notification's Action Cable returns API response as gem's default function, please share your expected format with us. Thanks!

@farooqch11
Copy link
Author

farooqch11 commented Sep 2, 2019

I added this file under config/initializers/activity_notification_ext.rb

and put this code in this file, works perfectly fine for me!

i will broadcast JSON instead of html whenever action cable is called for notification!

require "activity_notification/apis/notification_api.rb"

ActivityNotification::NotificationApi.module_eval do
  if Rails::VERSION::MAJOR >= 5
    def broadcast_to_action_cable_channel(params = {})
      if target.notification_action_cable_allowed?(notifiable, key) &&
          notifiable.notification_action_cable_allowed?(target, key)
        target_channel_name = "#{ActivityNotification.config.notification_channel_prefix}_#{target_type}#{ActivityNotification.config.composite_key_delimiter}#{target_id}"
        index_options = params.slice(:filter, :limit, :without_grouping, :with_group_members, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key)
        ActionCable.server.broadcast(target_channel_name,
          JSON.parse(
            ApplicationController.render(
              template: "activity_notification/notifications/customers/_notification.json.jbuilder",
              locals: {notification: ActivityNotification::Notification.find(id)}
            )
          )    
        )
      end
    end
  end
end

@simukappu
Copy link
Owner

Thank you for your feedback!
Working in progress to create api mode for activity_notification.

@simukappu simukappu added this to the API mode milestone Sep 28, 2019
@simukappu simukappu removed the need more info We need more information label Sep 29, 2019
@irondnb
Copy link

irondnb commented Nov 27, 2019

I went easy way and implemented broadcasting as optional target before version 2.0
@simukappu are you going to add some way to turn off/on broadcasting with target subscription?

class UserSocket < ActivityNotification::OptionalTarget::Base
  def notify(notification, options = {})
    NotificationBroadcastJob.perform_later(notification)
  end
end

class NotificationBroadcastJob < ApplicationJob
  queue_as :default

  def perform(notification)
    ActionCable.server.broadcast(
      "notifications-#{notification.target.id}",
      notification: JSON.parse(notification_details(notification))
    )
  end

  def notification_details(notification)
    ApplicationController.render(
      partial: 'activity_notification/notifications_with_devise/notification',
      locals: { notification: notification }
    )
  end
end

@simukappu
Copy link
Owner

Hi @irondnb,
As you mentioned, I am considering moving this internal implementation for ActionCable broadcasting to optional targets. Then, we can create broadcasting returning JSON response or that integrated with authentication.
With broadcasting using optional targets, we can manage subscription by Subscription#subscribe_to_optional_target and so on.

@simukappu
Copy link
Owner

Released as v2.1.0. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants