module ActionCable::Channel::PeriodicTimers::ClassMethods

Public Instance Methods

periodically(callback_or_method_name = nil, every:, &block) Show source
# File actioncable/lib/action_cable/channel/periodic_timers.rb, line 30
def periodically(callback_or_method_name = nil, every,, &block)
  callback =
    if block_given?
      raise ArgumentError, 'Pass a block or provide a callback arg, not both' if callback_or_method_name
      block
    else
      case callback_or_method_name
      when Proc
        callback_or_method_name
      when Symbol
        -> { __send__ callback_or_method_name }
      else
        raise ArgumentError, "Expected a Symbol method name or a Proc, got #{callback_or_method_name.inspect}"
      end
    end

  unless every.kind_of?(Numeric) && every > 0
    raise ArgumentError, "Expected every: to be a positive number of seconds, got #{every.inspect}"
  end

  self.periodic_timers += [[ callback, every: every ]]
end

Periodically performs a task on the channel, like updating an online user counter, polling a backend for new status messages, sending regular “heartbeat” messages, or doing some internal work and giving progress updates.

Pass a method name or lambda argument or provide a block to call. Specify the calling period in seconds using the every: keyword argument.

periodically :transmit_progress, every: 5.seconds

periodically every: 3.minutes do
  transmit action: :update_count, count: current_count
end

© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.