aboutsummaryrefslogblamecommitdiffstats
path: root/lib/action_cable/channel/redis.rb
blob: fc25f1dd4c212b94ba4eb768b135d492c0cf40cb (plain) (tree)
1
2
3
4
5
6
7
8
9







                                                       
                                         


                                                     
                                                                                   
                               

                                                       
                                                                                      
                                                  




                                           
                                                                                                    

             



       
module ActionCable
  module Channel

    module Redis
      extend ActiveSupport::Concern

      included do
        on_unsubscribe :unsubscribe_from_redis_channels
        delegate :pubsub, to: :connection
      end

      def subscribe_to(redis_channel, callback = nil)
        callback ||= -> (message) { broadcast ActiveSupport::JSON.decode(message) }
        @_redis_channels ||= []
        @_redis_channels << [ redis_channel, callback ]

        logger.info "[ActionCable] Subscribing to the redis channel: #{redis_channel}"
        pubsub.subscribe(redis_channel, &callback)
      end

      protected
        def unsubscribe_from_redis_channels
          if @_redis_channels
            @_redis_channels.each { |channel, callback| pubsub.unsubscribe_proc(channel, callback) }
          end
        end
    end

  end
end