Reputation: 1608
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?
Upvotes: 3
Views: 854
Reputation: 11
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.
Upvotes: 1