Class: Firehose::Rack::HttpLongPoll

Inherits:
Object
  • Object
show all
Defined in:
lib/firehose/rack.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/firehose/rack.rb', line 8

def call(env)
  req     = ::Rack::Request.new(env)
  cid     = req.params['cid']
  path    = req.path
  method  = req.request_method

  case method
  # GET is how clients subscribe to the queue. When a messages comes in, we flush out a response,
  # close down the requeust, and the client then reconnects.
  when 'GET'
    EM.next_tick do
      subscription = Firehose::Subscription.new(cid)
      subscription.subscribe path do |payload|
        subscription.unsubscribe
        env['async.callback'].call([200, {}, [payload]])
      end
    end

    Firehose::Rack::AsyncResponse

  # PUT is how we throw messages on to the fan-out queue.
  when 'PUT'
    body = env['rack.input'].read
    p [:put, path, body]
    Firehose::Publisher.new.publish(path, body)

    [202, {}, []]
  else
    [501, {}, ["#{method} not supported."]]
  end
end