Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stream a broadcast to a specific user in Rails 5 with ActionCable?

I have 2 user types in my application (worker and company). Both user types are created with Devise. I'm currently trying to send a notification to a specific company with ActionCable.

My main problem is that when I send a notification every single company that's signed in receives it. I get that I'm supposed to include the company id in the stream name in some way, but I haven't had any luck so far.

I've included the working code that sends notifications to all companies below:

notifications_channel.rb

class NotificationsChannel < ApplicationCable::Channel
  def subscribed
    stream_from "notifications_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end

Call to the broadcast

ActionCable.server.broadcast 'notifications_channel', { 'My data' }

EDIT

I log the status of the notification with javascript:

notifications.js

App.notifications = App.cable.subscriptions.create("NotificationsChannel", {
  connected: function() {
    console.log("connected");
  };

  disconnected: function() {
    console.log("disconnected");
  };

  received: function(data) {
    console.log("recieved");
  };
});
like image 780
Tobias Kristensen Avatar asked Nov 27 '18 03:11

Tobias Kristensen


2 Answers

Broadcast the message from your controller like this:

# Broadcast your message
ActionCable.server.broadcast "notifications_channel:#{target_user.id}

Now Update app/channels/application_cable/connection.rb with the below code

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.name
    end

    protected

    def find_verified_user
      verified_user = User.find_by(id: cookies.signed['user.id'])
      if verified_user && cookies.signed['user.expires_at'] > Time.now
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

And subscribe to the stream like this:

def subscribed
  stream_from "notifications_channel:#{current_user.id}"
end

Note: This is just an example to show how to target a specific user in Actioncable. You may have to modify the code based on your requirement.

I also recommend watching this video by GoRails.

like image 129
Abhilash Reddy Avatar answered Oct 19 '22 01:10

Abhilash Reddy


I managed to find the solution. Following Abhilashs answer took me most of the way, but I still had trouble authenticating the company. It seems like Warden was not fully configured, so this post made it work: env['warden'] not working with Rails 5

like image 42
Tobias Kristensen Avatar answered Oct 19 '22 03:10

Tobias Kristensen