0

I am getting a error using action cable,

NameError (undefined local variable or method `connections_info' for MicropostNotificationsChannel:Class):

app/channels/micropost_notifications_channel.rb:12:in `notify'
app/models/notification.rb:8:in `block in <class:Notification>'
app/controllers/likes_controller.rb:11:in `create'
Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
...

To my knowledge and from using controllers and models I can call the class method after_commit -> {MicropostNotificationsChannel.notify(self)} and then get to the self.notifiy(notification) and then as connections_info is a instance method I should be able to call it inside that class and carry out my code but I get the error here?

class Notification < ApplicationRecord
  ...
  after_commit -> {MicropostNotificationsChannel.notify(self)}
end

The action cable micropost notification channel

class MicropostNotificationsChannel < ApplicationCable::Channel

  def subscribe
    ...
  end

  def unsubscribe
    ...
  end

  def self.notifiy(notification)
    connection_results = connections_info
    puts connection_results.inspect
  end
end

Channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base

    def connections_info
      # do stuff here
    end
  end
end
Lee Eather
  • 345
  • 3
  • 16

2 Answers2

2

You've defined connections_info as an instance method in ApplicationCable::Channel, but notify is a class method and so it's looking for methods at the class level instead of the instance level. Sometimes classes will get around this by using method_missing, but it doesn't look like Action Cable does that at a glance. Without knowing more about what you're trying to do, it's hard to say whether to change connections_info to a class method, notify to an instance method, or something else.

Max
  • 1,817
  • 1
  • 10
  • 13
  • `connections_info` is getting all the user objects that `ActionCable` has stored so I can get who is on what channel and SAVE notification records to the db as well as push dynamic notifications. I am able to call `notfify` as a instance method from `subscribe` when a connection is established. But I do not want to call it from there. I never knew that you had to call a instance method from within a instance method not a class method. Is there a reason for that? – Lee Eather Oct 07 '18 at 04:00
  • Sorry I think i get that now. Thanks you! Am I able to ask, I would need to get a hold of the ActionCable instance that is used? – Lee Eather Oct 07 '18 at 04:04
  • @LeeEather I'm not super familiar with Action Cable so it might make sense to ask a separate question. But looking at https://guides.rubyonrails.org/action_cable_overview.html, it looks like the recommended way to send notifications is to call `stream_from` or `stream_for` in the `subscribed` callback and then call `MicropostNotificationsChannel.broadcast_to(...)`. You don't necessarily need to know who all the users are at any one time unless you're doing something more advanced. – Max Oct 08 '18 at 02:08
  • Thanks @max. I have set up action cable like you said, but it is more advanced. I want to create notifications for when a user subscribes to a post, so not only like you said use `stream_from` to connect the user/s to the post I also want to record a record in the db for each user connected when the notification is created. So trying to tap into the ActionCable instance is what I am looking for, it keeps a copy on the client side for each user but looking into how I can get the `MicropostNotificationsChannel` instance. Doesnt help I am still novice at ruby, but I'll see how I go! – Lee Eather Oct 08 '18 at 03:40
  • @LeeEather Yeah, probably best to ask a new question focusing on that. You might get some people with more Action Cable experience than me to weigh in! – Max Oct 08 '18 at 14:56
  • Yeah np. I was able to solve this as you said, using class methods and switched from a instance of the `connection.server` method I was using, to ActionCables singleton method of the server `ActionCable.server` – Lee Eather Oct 09 '18 at 03:51
0

This also happens when your redis configuration is not correct.

Baran Yeni
  • 314
  • 5
  • 14