SSE / Redis / Goliath coexisting with API / Map replacement

35 views
Skip to first unread message

Claudio Villalobos

unread,
Jul 8, 2014, 1:02:03 PM7/8/14
to golia...@googlegroups.com
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

Claudio Villalobos

unread,
Jul 8, 2014, 1:17:35 PM7/8/14
to golia...@googlegroups.com
Worked using this on each request:

  env['redis_sub'] = Redis.new({driver: :synchrony})

Would this be the right way of doing it?

Thanks

Eric Marden

unread,
Jul 8, 2014, 4:41:50 PM7/8/14
to golia...@googlegroups.com
You might also take a look at using em-redis for your redis connections: https://github.com/madsimian/em-redis


--
Eric Marden
http://ericmarden.com
--------------------------------
PGP Public Key: http://xentek.net/xentek.gpg
> --
> You received this message because you are subscribed to the Google Groups "Goliath.io (http://Goliath.io)" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to goliath-io+...@googlegroups.com (mailto:goliath-io+...@googlegroups.com).
> For more options, visit https://groups.google.com/d/optout.



Message has been deleted

Claudio Villalobos

unread,
Jul 9, 2014, 9:56:15 AM7/9/14
to golia...@googlegroups.com
Looks like no updates to that project in 4 years :S
> To unsubscribe from this group and stop receiving emails from it, send an email to goliath-io+...@googlegroups.com (mailto:goliath-io+unsub...@googlegroups.com).
Reply all
Reply to author
Forward
0 new messages