I would like to achieve following behavior, having two applications. Whenever user of one application makes an entry in the database, I would like to inform the client who is using different application, database is shared. I am using rails. To achieve this I have tried to use redis pub/sub, and server side events, it worked but every time client was moving from page to another one new connection to redis was spawned and the old wasn't released which caused server to crash. Is there any other suggestions how this issue can be solved.
class EventController < ApplicationController
include ActionController::Live
def index
ActiveRecord::Base.clear_active_connections!
response.headers['Content-Type'] = 'text/event-stream'
redis = Redis.new
@last_active = Time.zone.now
redis.subscribe('event', 'heartbeat') do |on|
on.message do |pattern, event, data|
if event == 'heartbeat'
idle_time = (Time.zone.now - @last_active).to_i
if idle_time > 2.minutes
redis.unsubscribe
end
else
@last_active = Time.zone.now
end
response.stream.write(data, event: 'new:verification')
end
end
rescue IOError
rescue ClientDisconnected
ensure
redis.quit
response.stream.close
end
end
I also have tried multiple implementation from this thread Redis + ActionController::Live threads not dying