Skip to content

Commit

Permalink
README: use long polling to exemplify streaming API.
Browse files Browse the repository at this point in the history
[Long polling][1] is a simple, practical use case of `stream` helper with the
optional `keep_open` parameter.  Let's use it to exemplify streaming API, so it
is easier to understand and use.

[1]: http://en.wikipedia.org/wiki/Push_technology#Long_polling

Signed-off-by: Anurag Priyam <anurag08priyam@gmail.com>
  • Loading branch information
yeban committed Dec 22, 2012
1 parent a8de3f2 commit d4ae7f0
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions README.rdoc
Expand Up @@ -897,18 +897,33 @@ the stream object, allowing you to close it at any later point in the
execution flow. This only works on evented servers, like Thin and Rainbows.
Other servers will still close the stream:

# long polling

set :server, :thin
connections = []

get '/' do
# keep stream open
get '/subscribe' do
# register a client's interest in server events
stream(:keep_open) { |out| connections << out }

# purge dead connections
connections.reject!(&:closed?)

# acknowledge
"subscribed"
end

post '/' do
# write to all open streams
connections.each { |out| out << params[:message] << "\n" }
"message sent"
post '/message' do
connections.each do |out|
# notify client that a new message has arrived
out << message << "\n"

# indicate client to connect again
out.close
end

# acknowledge
"message received"
end

=== Logging
Expand Down

0 comments on commit d4ae7f0

Please sign in to comment.