Hello,
I am working on something similar to this:
And integrating it under /events to my existing App API- The issue is that on this example @redis stays within each Subscribe class.
and since I cannot use map anymore, I cannot create an instance for each request... I am not sure how to keep each subscribed socket alive and connected to redis.
I came down to roughly this... but ofc its a shared connection for all subsequent requests, so it does not work.
class ApiServer < Goliath::API
def response(env)
if env['PATH_INFO'] == "/events" && env.params["channel"]
EM.synchrony do
@redis_conn = Redis.new({driver: :synchrony})
@redis_conn.subscribe(channel) do |on|
on.message do |channel, message|
env.stream_send("data:#{message}\n\n")
end
end
end
streaming_response(200, {'Content-Type' => 'text/event-stream'})
else
MyAPI::API.call(env)
end
end
end
runner = Goliath::Runner.new(ARGV, nil)
runner.api = ApiServer.new
runner.app = Goliath::Rack::Builder.build(ApiServer, runner.api)
runner.run
Any ideas will be greatly appreciated it.
Thanks