Trying to send an event from within Rufus Schedulers results in this error:
{ 70231586340180 rufus-scheduler intercepted an error:
70231586340180 job:
70231586340180 Rufus::Scheduler::IntervalJob "2s" {}
70231586340180 error:
70231586340180 70231586340180
70231586340180 NameError
70231586340180 undefined local variable or method `connections' for main:Object
70231586340180 picture-frame.rb:31:in `send_event'
70231586340180 picture-frame.rb:24:in `block in <main>'
...
It seems like I can't access connections
from within the Rufus Job. How can I fix this? This is my code:
require 'sinatra'
require 'rufus-scheduler'
require 'json'
set :server, :thin
connections = []
scheduler = Rufus::Scheduler.new
get '/' do
erb :index
end
get '/events', provides: 'text/event-stream' do
stream :keep_open do |out|
connections << out
connections.reject!(&:closed?)
end
end
scheduler.interval '2s' do
random = (0...8).map { (65 + rand(26)).chr }.join
send_event('random', { random: random })
end
def send_event(id, body)
body[:id] = id
event = "data: #{body.to_json}"
connections.each { |out| out << event }
end