Tech Tips

  1. Uncategorized
  2. 76 view

[StreamingAPI][Ruby]Try to build Streaming API with Ruby

Contents

Github Repository

https://github.com/zuqqhi2/streamingapi

Overview

Try to build Streaming API with rack-stream library.

Environment

OS

Linux www4322gi 3.2.0-64-generic #97-Ubuntu SMP Wed Jun 4 22:04:21 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

Ruby

rbenv(ruby version : ruby 1.9.3p547 (2014-05-14 revision 45962) [x86_64-linux])

Code

Same as rack-stream’s github code.

Gemfile

Use thin as Web server.
source 'https://rubygems.org'

gem 'thin'
gem 'rack-stream'

config.ru

#  bundle exec thin start -R config.ru -p 9292
# config.ru
# run with `thin start -p 9292`
require 'rack/stream'

class App
  include Rack::Stream::DSL

  stream do
    after_open do
      count = 0
      @timer = EM.add_periodic_timer(1) do
        if count != 3
          chunk "chunky #{count}\n"
          count += 1
        else
          # Connection isn't closed until #close is called.
          # Useful if you're building a firehose API
          close
        end
      end
    end

    before_close do
      @timer.cancel
      chunk "monkey!\n"
    end

    [200, {'Content-Type' => 'text/plain'}, []]
  end
end

app = Rack::Builder.app do
  use Rack::Stream
  run App.new
end

run app

Result

# Server
$ bundle exec thin start -R config.ru -p 9292
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on 0.0.0.0:9292, CTRL+C to stop

# Client
curl http://localhost:9292
chunky 0
chunky 1
chunky 2
monkey!

Github Repository

https://github.com/zuqqhi2/streamingapi

Uncategorized recent post

  1. Run Amazon FreeRTOS on M5Stack Core2 for AWS …

  2. Udacity Self-Driving Car Engineer Nanodegree …

  3. Install sbt 1.0.0 and run sample template

  4. Visualization of Neural Network and its Train…

  5. [Machine Learning]Created docker image includ…

関連記事

PAGE TOP