ChronoTrigger is designed to work with Rails 5.2 and beyond. It has minimal gem dependencies and no infrastructure dependencies.
bundleaddchrono_triggermkdirapp/events
In the app/events folder, we will create an ApplicationEvent class for our Event classes to inherit from:
app/events/application_event.rb
classApplicationEvent<ChronoTrigger::Eventinclude CableReady::Broadcaster # delegate :render, to: ApplicationControllerend
While you can use ChronoTrigger to run any code, it has been designed to work with CableReady. Including CableReady::Broadcaster makes the cable_ready method available to all of your Event classes.
If you plan to use CableReady to send rendered HTML, you might wish to delegate render to ApplicationController.
Finally, you need to start the Clock! The best place to do this is after Puma starts up:
config/puma.rb
# place this at the bottom of puma.rbChronoTrigger::Clock.start
Optional: Create an initializer
Today, it's possible to configure the resolution of the timer, but the 100ms default is likely fine. There will be additional options added in the future:
You might be wondering: why not start the Clock in the initializer? The reason is that you only want to run ChronoTrigger in the web server process - not Sidekiq, console, when running migrations and other rake tasks...