Search code examples
ruby-on-railspublish-subscribewisper

Wisper: Subscribers not hearing broadcasts


I was recommend in an earlier question to use a gem called Wisper. I am very happy to learn about it, as it is exactly the solution I'm looking for. What I can't understand from the documentation on Wisper is how listeners register themselves.

Here is my code:

app/models/subscription.rb

class Subscription < ActiveRecord::Base
include Wisper::Publisher

  def some_method
    # some code here
    broadcast(:subscription_paused)
  end
end

app/models/offer.rb

class Offer < ActiveRecord::Base
  def subscription_paused
    binding.pry # or whatever
  end
end

So I'm not entirely sure about this part here. I've tried a variety of subscribing techniques, but I think it just comes down to me not really understanding this aspect of it:

config/initializers/wisper.rb

Wisper.subscribe(Offer.new)

I also tried, similar to the example in the Wiki:

subscription = Subscription.new
subscription.subscribe(Offer.new)

What am I missing here? (I'm not really sure if the above code should even go in an initializer.)


Solution

  • If the tables exists for Offer and Subscription model then the code should work.

    Try this in the rails console:

    # class Subscription < ActiveRecord::Base
    class Subscription
      include Wisper::Publisher
    
      def some_method
        # some code here
        broadcast(:subscription_paused)
      end
    end
    
    # class Offer < ActiveRecord::Base
    class Offer
      def subscription_paused
        puts "jeijjj"
      end
    end
    
    
    Wisper.subscribe(Offer.new)
    
    Subscription.new.some_method
    

    It should generate an output:

    "jeijjj"