Sha256: 1bb9a263449786da9e2e0b9bfd33eaf87b435c1bed789b214d54b2a847543816

Contents?: true

Size: 996 Bytes

Versions: 11

Compression:

Stored size: 996 Bytes

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::Params

  use Goliath::Rack::Render, 'json'
  use Goliath::Rack::Heartbeat
  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

11 entries across 11 versions & 1 rubygems

Version Path
goliath-1.0.7 examples/content_stream.rb
goliath-1.0.6 examples/content_stream.rb
goliath-1.0.5 examples/content_stream.rb
goliath-1.0.4 examples/content_stream.rb
goliath-1.0.3 examples/content_stream.rb
goliath-1.0.2 examples/content_stream.rb
goliath-1.0.1 examples/content_stream.rb
goliath-1.0.0 examples/content_stream.rb
goliath-1.0.0.beta.1 examples/content_stream.rb
goliath-0.9.4 examples/content_stream.rb
goliath-0.9.2 examples/content_stream.rb