Skip to content

Instantly share code, notes, and snippets.

@rkh
Forked from blambeau/Gemfile
Created December 14, 2011 11:16
Show Gist options
  • Select an option

  • Save rkh/1476182 to your computer and use it in GitHub Desktop.

Select an option

Save rkh/1476182 to your computer and use it in GitHub Desktop.

Revisions

  1. rkh revised this gist Dec 14, 2011. 2 changed files with 4 additions and 2 deletions.
    3 changes: 2 additions & 1 deletion app.rb
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,10 @@
    end

    get '/bottles' do
    content_type "text/event-stream"
    stream do |out|
    1000.times do |i|
    out << "#{i} bottle(s) on a wall...\n"
    out << "data: #{i} bottle(s) on a wall...\n\n"
    sleep 0.5
    end
    end
    3 changes: 2 additions & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,8 @@ <h1>Welcome to this Sinatra Streaming example!</h1>
    </pre>
    <script>
    $("#bottles-button").click(function() {
    // please code the streaming functionnality here
    var src = new EventSource('/bottles');
    src.onmessage = function(e) { $('#bottles-output').append("\n" + e.data) };
    });
    </script>
    </body>
  2. @blambeau blambeau revised this gist Dec 14, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    A server-side service outputs a long response using Sinatra's streaming API.
    How can the javascript client process this long response as a stream and refresh
    a <code><pre></pre></code> in the HTML page?
    a pre in the HTML page until the whole response has been received?

    To run it:

  3. @blambeau blambeau revised this gist Dec 14, 2011. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    # jQuery + Sinatra + Streaming

    A server-side service outputs a long response using Sinatra's streaming API.
    How can the javascript client process this long response as a stream and refresh
    a <code><pre></pre></code> in the HTML page?

    To run it:

    bundle install
  4. @blambeau blambeau revised this gist Dec 14, 2011. 1 changed file with 0 additions and 24 deletions.
    24 changes: 0 additions & 24 deletions Gemfile.lock
    Original file line number Diff line number Diff line change
    @@ -1,24 +0,0 @@
    GEM
    remote: http://rubygems.org/
    specs:
    daemons (1.1.4)
    eventmachine (0.12.10)
    rack (1.3.5)
    rack-protection (1.1.4)
    rack
    sinatra (1.3.1)
    rack (~> 1.3, >= 1.3.4)
    rack-protection (~> 1.1, >= 1.1.2)
    tilt (~> 1.3, >= 1.3.3)
    thin (1.3.1)
    daemons (>= 1.0.9)
    eventmachine (>= 0.12.6)
    rack (>= 1.0.0)
    tilt (1.3.3)

    PLATFORMS
    ruby

    DEPENDENCIES
    sinatra (~> 1.3.0)
    thin
  5. @blambeau blambeau revised this gist Dec 14, 2011. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # jQuery + Sinatra + Streaming

    To run it:

    bundle install
    bundle exec ruby app.rb

    Then, try to implement the requested feature. See index.html
  6. @blambeau blambeau revised this gist Dec 14, 2011. 4 changed files with 63 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion Gemfile
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    source "http://rubygems.org/"
    gem "sinatra", "~> 1.3.0"
    gem "thin"
    gem "thin"
    24 changes: 24 additions & 0 deletions Gemfile.lock
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    GEM
    remote: http://rubygems.org/
    specs:
    daemons (1.1.4)
    eventmachine (0.12.10)
    rack (1.3.5)
    rack-protection (1.1.4)
    rack
    sinatra (1.3.1)
    rack (~> 1.3, >= 1.3.4)
    rack-protection (~> 1.1, >= 1.1.2)
    tilt (~> 1.3, >= 1.3.3)
    thin (1.3.1)
    daemons (>= 1.0.9)
    eventmachine (>= 0.12.6)
    rack (>= 1.0.0)
    tilt (1.3.3)

    PLATFORMS
    ruby

    DEPENDENCIES
    sinatra (~> 1.3.0)
    thin
    16 changes: 16 additions & 0 deletions app.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    require 'sinatra'

    set :server, :thin

    get '/' do
    send_file "index.html"
    end

    get '/bottles' do
    stream do |out|
    1000.times do |i|
    out << "#{i} bottle(s) on a wall...\n"
    sleep 0.5
    end
    end
    end
    22 changes: 22 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <!DOCTYPE html>
    <html lang="fr">
    <head>
    <title>Sinatra streaming example</title>
    <meta charset="utf-8" />
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    </head>
    <body>
    <h1>Welcome to this Sinatra Streaming example!</h1>
    <input id="bottles-button" type="button" value="Bottles!"/>
    <pre id="bottles-output">
    </pre>
    <script>
    $("#bottles-button").click(function() {
    // please code the streaming functionnality here
    });
    </script>
    </body>
    </html>
  7. @blambeau blambeau created this gist Dec 14, 2011.
    3 changes: 3 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    source "http://rubygems.org/"
    gem "sinatra", "~> 1.3.0"
    gem "thin"