Daniel Gruno 4 tahun lalu
induk
melakukan
71d45f6a80
1 mengubah file dengan 48 tambahan dan 0 penghapusan
  1. 48 0
      README.md

+ 48 - 0
README.md

@@ -12,6 +12,7 @@
   * [Listening for events via cURL](#listening-for-events-via-curl)
   * [Listening for events via Python](#listening-for-events-via-python)
   * [Listening for events via node.js](#listening-for-events-via-nodejs)
+  * [Listening for events via Ruby](#listening-for-events-via-ruby)
   * [Accessing older payloads via the backlog catalogue](#accessing-older-payloads-via-the-backlog-catalogue)
 - [Access-Control-List and private events](#access-control-list-and-private-events)
   * [Pushing a private event](#pushing-a-private-event)
@@ -128,6 +129,53 @@ const pps = new PyPubSub('http://localhost:2069/');
 pps.attach(process);
 ~~~
 
+### Listening for events via Ruby
+Likewise, using Ruby is a pretty straightforward case:
+
+~~~ruby
+require 'net/http'
+require 'json'
+require 'thread'
+
+pubsub_URL = 'http://localhost:2069/'
+
+def do_stuff_with(event)
+  print("Got a pubsub event!:\n")
+  print(event)
+end
+
+def listen(url)
+  ps_thread = Thread.new do
+    begin
+      uri = URI.parse(url)
+      Net::HTTP.start(uri.host, uri.port) do |http|
+        request = Net::HTTP::Get.new uri.request_uri
+        http.request request do |response|
+          body = ''
+          response.read_body do |chunk|
+            event = JSON.parse(chunk)
+            if event['stillalive']  # pingback
+              print("ping? PONG!\n")
+            else
+              do_stuff_with(event)
+            end
+          end
+        end
+      end
+  end
+  return ps_thread
+end
+
+begin
+  ps_thread = listen(pubsub_URL)
+  print("Pubsub thread started, waiting for results...")
+  while ps_thread.alive?
+    sleep 10
+  end
+  print("Pubsub thread died :(\n")
+end
+~~~
+
 ### Accessing older payloads via the backlog catalogue
 If configured, via the `payload_backlog_size` setting in the main configuration, clients can 
 request payloads that were pushed before they subscribed, using an `X-Fetch-Since` request