Sha256: e13ae90b54e7b00592e42862c3aa0e26acec2a3c20df38d0216f132821637493

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

#!/usr/bin/env ruby
$:<< '../lib' << 'lib'

require 'goliath'

# This example assumes you have an AMQP server up and running with the
# following config (using rabbit-mq as an example)
#
# rabbitmq-server start
# rabbitmqctl add_vhost /test
# rabbitmqctl add_user test test
# rabbitmqctl set_permissions -p /test test ".*" ".*" ".*"

class ContentStream < Goliath::API
  use Goliath::Rack::Formatters::JSON
  use Goliath::Rack::Params

  use Goliath::Rack::Render
  use Goliath::Rack::Heartbeat
  use Goliath::Rack::ValidationError
  use Goliath::Rack::Validation::RequestMethod, %w(GET)

  def on_close(env)
    # This is just to make sure if the Heartbeat fires we don't try
    # to close a connection.
    return unless env['subscription']

    env.channel.unsubscribe(env['subscription'])
    env.logger.info "Stream connection closed."
  end

  def response(env)
    env.logger.info "Stream connection opened"

    env['subscription'] = env.channel.subscribe do |msg|
      env.stream_send(msg)
    end

    [200, {}, Goliath::Response::STREAMING]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
goliath-0.9.1 examples/content_stream.rb