Permalink
Browse files

README: use long polling to exemplify streaming API.

[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...
1 parent a8de3f2 commit d4ae7f0a22b118c84a002b6e364400ce0043fb76 @yeban yeban committed Dec 22, 2012
Showing with 21 additions and 6 deletions.
  1. +21 −6 README.rdoc
View
27 README.rdoc
@@ -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

0 comments on commit d4ae7f0

Please sign in to comment.