Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionCable unable to call on periodic timers

This is not documented in Rails, but ActionCable has a module called PeriodicTimers https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/channel/periodic_timers.rb

Now in my app/channels/requests_channel.rb I have attempted to test this out by doing:

class RequestsChannel < ApplicationCable::Channel
  periodically :transmit_status, every: 2.seconds

  def subscribed
      stream_from "requests_#{current_user.id}"
  end

  def unsubscribed
  end

  private

  def transmit_status
    puts "ping"
  end
end

and when I connect and subscribe to the stream, nothing shows up in the console. I felt like the code in ActionCable was fairly straight forward, and that this should work but I'm unable to get it to work. Anybody have any experience with this?

like image 853
Sebastian Jennings Almnes Avatar asked Dec 13 '25 21:12

Sebastian Jennings Almnes


1 Answers

In Rails 5.2.0, both

  periodically every: 5.seconds do
    transmit payload: 'some_payload'
  end

And,

periodically :transmit_device_status, every: 5.seconds

def transmit_device_status
  transmit payload: 'some_payload'
end

Worked for me on the first try. I can't tell you why it wasn't working for you initially sorry, but it might help someone else since this is one of the very few posts I found on the subject.

like image 181
SGjunior Avatar answered Dec 15 '25 14:12

SGjunior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!