Skip to content
This repository
Browse code

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...
commit d4ae7f0a22b118c84a002b6e364400ce0043fb76 1 parent a8de3f2
Anurag Priyam authored December 22, 2012

Showing 1 changed file with 21 additions and 6 deletions. Show diff stats Hide diff stats

  1. 27  README.rdoc
27  README.rdoc
Source Rendered
@@ -897,18 +897,33 @@ the stream object, allowing you to close it at any later point in the
897 897
 execution flow. This only works on evented servers, like Thin and Rainbows.
898 898
 Other servers will still close the stream:
899 899
 
  900
+  # long polling
  901
+
900 902
   set :server, :thin
901 903
   connections = []
902 904
 
903  
-  get '/' do
904  
-    # keep stream open
  905
+  get '/subscribe' do
  906
+    # register a client's interest in server events
905 907
     stream(:keep_open) { |out| connections << out }
  908
+
  909
+    # purge dead connections
  910
+    connections.reject!(&:closed?)
  911
+
  912
+    # acknowledge
  913
+    "subscribed"
906 914
   end
907 915
 
908  
-  post '/' do
909  
-    # write to all open streams
910  
-    connections.each { |out| out << params[:message] << "\n" }
911  
-    "message sent"
  916
+  post '/message' do
  917
+    connections.each do |out|
  918
+      # notify client that a new message has arrived
  919
+      out << message << "\n"
  920
+
  921
+      # indicate client to connect again
  922
+      out.close
  923
+    end
  924
+
  925
+    # acknowledge
  926
+    "message received"
912 927
   end
913 928
 
914 929
 === Logging

0 notes on commit d4ae7f0

Please sign in to comment.
Something went wrong with that request. Please try again.