ruby-on-railswisper

reload wisper listeners automatically at every request


I have wisper listeners in the app/listeners directory.

I also have the /config/initializers/wisper.rb

module Wisper
  def self.setup
    configure do |config|
      config.broadcaster(:default, Broadcasters::LoggerBroadcaster.new(Rails.logger, Broadcasters::SendBroadcaster.new))
    end
  end
end

Wisper.setup

Wisper.subscribe(ProjectListener.new)
Wisper.subscribe(FeedListener.new)

Can I somehow force Rails to reload the Listeners at every request?


Solution

  • You could try wrapping the subscribes in a to_prepare block, something like:

    Rails.application.config.to_prepare do
      Wisper.clear if Rails.env.development?
      Wisper.subscribe(ProjectListener.new)
      Wisper.subscribe(FeedListener.new)
    end
    

    to_prepare is called once in production and before every request in development environment.

    If you subscribe in more than one initializer you could put the Wisper.clear in an initializer named '01_clear_subscribers` to ensure the subscribers are only cleared once.

    Incidentally you don't need to override setup to configure the broadcaster, just do Wisper.configure do |config|.