Permalink
Browse files

Merge pull request #599 from yeban/update_stream_documentation

Update stream documentation
  • Loading branch information...
2 parents a8de3f2 + 248e0ea commit 08373e8890a85459c0326ebd98b1292fcc79ec80 @rkh rkh committed Dec 22, 2012
Showing with 22 additions and 7 deletions.
  1. +21 −6 README.rdoc
  2. +1 −1 lib/sinatra/base.rb
View
@@ -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
View
@@ -289,7 +289,7 @@ def send_file(path, opts={})
# Class of the response body in case you use #stream.
#
# Three things really matter: The front and back block (back being the
- # blog generating content, front the one sending it to the client) and
+ # block generating content, front the one sending it to the client) and
# the scheduler, integrating with whatever concurrency feature the Rack
# handler is using.
#

0 comments on commit 08373e8

Please sign in to comment.