meerkat 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- meerkat (0.4.0)
4
+ meerkat (0.4.1)
5
5
  em-hiredis
6
6
  eventmachine
7
7
  pg
data/examples/app.rb CHANGED
@@ -2,12 +2,13 @@ require 'sinatra/base'
2
2
  require 'haml'
3
3
 
4
4
  class App < Sinatra::Base
5
- get '*' do
6
- @route = params[:splat].join
5
+ get '/' do
7
6
  haml :index
8
7
  end
9
- post '*' do
10
- Meerkat.publish params[:splat].join, params[:message]
8
+
9
+ post '/' do
10
+ puts params
11
+ Meerkat.publish params[:topic], params[:message]
11
12
  204
12
13
  end
13
14
  end
@@ -6,27 +6,47 @@
6
6
  %fieldset
7
7
  %legend Publish
8
8
  %form#pub{:method => :post}
9
- %input{:name => 'message'}
10
- %input{:type=>'submit', :value => 'Send'}
9
+ %label
10
+ Topic:
11
+ %input{:name => 'topic', :value => 'foo'}
12
+ %label
13
+ Message:
14
+ %input{:name => 'message'}
15
+ %input{:type => 'submit', :value => 'Send'}
11
16
 
12
17
  %fieldset
13
18
  %legend Subscribe
14
- %p
15
- Subscribing to
16
- %strong= @route
19
+ %label
20
+ Subscribe to
21
+ %input#sub_topic{:name => 'topic', :value => 'foo'}
22
+ %input#start_subscribe{:type => 'submit', :value => 'Start!'}
23
+
17
24
  %ul#stream
25
+
18
26
  :javascript
19
- var source = new EventSource('/stream#{@route}');
20
- var stream = document.getElementById('stream');
21
- source.addEventListener('message', function(e) {
22
- var li = document.createElement('li');
23
- li.innerHTML = JSON.parse(e.data);
24
- stream.appendChild(li);
25
- }, false);
27
+ var source;
28
+ function subscribe_to(topic) {
29
+ if (source != null) { source.close() }
30
+ source = new EventSource('/stream/' + topic)
31
+ var stream = document.getElementById('stream')
32
+ source.onmessage = function(e) {
33
+ console.log(e)
34
+ var li = document.createElement('li')
35
+ li.innerHTML = JSON.parse(e.data)
36
+ stream.appendChild(li)
37
+ }
38
+ }
26
39
 
27
40
  $(function() {
28
41
  $("#pub").submit(function() {
29
42
  $.post(this.action, $(this).serialize())
30
43
  return false
31
44
  });
45
+
46
+ $("#start_subscribe").click(function() {
47
+ subscribe_to($("#sub_topic").val())
48
+ return false
49
+ });
50
+
51
+ subscribe_to($("#sub_topic").val())
32
52
  })
@@ -4,12 +4,11 @@ module Meerkat
4
4
  module Backend
5
5
  class Redis
6
6
  def initialize(redis_uri = nil)
7
- @redis_uri = redis_uri
8
7
  @subs = {}
9
- EM.next_tick {
8
+ EM.next_tick do
10
9
  @sub = EM::Hiredis.connect redis_uri
11
10
  @pub = EM::Hiredis.connect redis_uri
12
- }
11
+ end
13
12
  end
14
13
 
15
14
  def publish(topic, json)
@@ -21,12 +20,12 @@ module Meerkat
21
20
  @subs[topic] << callback
22
21
  else
23
22
  @subs[topic] = [ callback ]
24
- EM.next_tick {
23
+ EM.next_tick do
25
24
  @sub.psubscribe topic
26
25
  @sub.on :pmessage do |topic, channel, message|
27
- @subs[topic].each { |c| c.call channel, message }
26
+ @subs[topic].each { |cb| cb.call channel, message }
28
27
  end
29
- }
28
+ end
30
29
  end
31
30
  [topic, callback]
32
31
  end
@@ -14,9 +14,11 @@ module Meerkat
14
14
  def call(env)
15
15
  body = DeferrableBody.new
16
16
 
17
- EM.next_tick {
18
- env['async.callback'].call [200, {'Content-Type' => 'text/event-stream'}, body]
17
+ headers = {
18
+ 'Content-Type' => 'text/event-stream',
19
+ 'X-Accel-Buffering' => 'no', # http://wiki.nginx.org/X-accel#X-Accel-Buffering
19
20
  }
21
+ EM.next_tick { env['async.callback'].call [200, headers, body] }
20
22
  EM.next_tick { body << "retry: #{@retry}\n" }
21
23
  EM.add_periodic_timer(@keep_alive) { body << ":\n" }
22
24
  EM.add_timer(@timeout) { body.succeed } if @timeout
@@ -26,7 +28,7 @@ module Meerkat
26
28
  body << "event: #{topic}\n"
27
29
  body << "data: #{json}\n\n"
28
30
  end
29
- env['async.close'].callback do
31
+ body.errback do
30
32
  Meerkat.unsubscribe sub
31
33
  end
32
34
 
@@ -1,3 +1,3 @@
1
1
  module Meerkat
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meerkat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-25 00:00:00.000000000Z
12
+ date: 2011-10-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70273991486880 !ruby/object:Gem::Requirement
16
+ requirement: &70352680163120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70273991486880
24
+ version_requirements: *70352680163120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack-test
27
- requirement: &70273991486440 !ruby/object:Gem::Requirement
27
+ requirement: &70352680162700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70273991486440
35
+ version_requirements: *70352680162700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: thin_async
38
- requirement: &70273991486000 !ruby/object:Gem::Requirement
38
+ requirement: &70352680162280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70273991486000
46
+ version_requirements: *70352680162280
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: thin-async-test
49
- requirement: &70273991485580 !ruby/object:Gem::Requirement
49
+ requirement: &70352680161860 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70273991485580
57
+ version_requirements: *70352680161860
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: em-minitest-spec
60
- requirement: &70273991485160 !ruby/object:Gem::Requirement
60
+ requirement: &70352680161420 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70273991485160
68
+ version_requirements: *70352680161420
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yajl-ruby
71
- requirement: &70273991484740 !ruby/object:Gem::Requirement
71
+ requirement: &70352680160960 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70273991484740
79
+ version_requirements: *70352680160960
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: eventmachine
82
- requirement: &70273991484320 !ruby/object:Gem::Requirement
82
+ requirement: &70352680160440 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70273991484320
90
+ version_requirements: *70352680160440
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: em-hiredis
93
- requirement: &70273991483900 !ruby/object:Gem::Requirement
93
+ requirement: &70352680159960 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *70273991483900
101
+ version_requirements: *70352680159960
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: pg
104
- requirement: &70273991483480 !ruby/object:Gem::Requirement
104
+ requirement: &70352680159360 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70273991483480
112
+ version_requirements: *70352680159360
113
113
  description: Requires an evented Ruby dispatcher, like Thin
114
114
  email:
115
115
  - carl.hoerberg@gmail.com
@@ -151,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  segments:
153
153
  - 0
154
- hash: -2704897263514401478
154
+ hash: 3801501159995886278
155
155
  required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  none: false
157
157
  requirements:
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  version: '0'
161
161
  segments:
162
162
  - 0
163
- hash: -2704897263514401478
163
+ hash: 3801501159995886278
164
164
  requirements: []
165
165
  rubyforge_project: meerkat
166
166
  rubygems_version: 1.8.6