sinatra-pubsub 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +36 -7
- data/examples/basic/Gemfile +6 -0
- data/examples/basic/app.rb +9 -0
- data/examples/{config.ru → basic/config.ru} +0 -0
- data/examples/{events.module.coffee → frontend/events.module.coffee} +0 -0
- data/examples/{stream.module.coffee → frontend/stream.module.coffee} +0 -0
- data/examples/tick/Gemfile +6 -0
- data/examples/{app.rb → tick/app.rb} +10 -4
- data/examples/tick/config.ru +3 -0
- data/lib/sinatra/pubsub/app.rb +3 -2
- data/lib/sinatra/pubsub/stream.rb +2 -2
- data/lib/sinatra/pubsub/version.rb +1 -1
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 678319b66eb37abf968fb94c42fb51f1afe76459
|
4
|
+
data.tar.gz: 2d9c574c2ca3bd5c8acc88f2810e8a3df79773b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: baa43672e636cff9ae1def170106ec98b181dc39a9be8d0a355687c519995cfc054546eec0743ee3a0132b3e8c23be1de30e20d13c815b859a05bf6bcaf90976
|
7
|
+
data.tar.gz: 0990f6d4555f7689b19f3998c106ac14261e1855cae7cd93d20fcb0246768f97306b5a9c16b659d302cf1d7408689dc527dde1e3a01ed27fe985ed4c242af04a
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -24,7 +24,7 @@ And publish to them:
|
|
24
24
|
|
25
25
|
## Requirements
|
26
26
|
|
27
|
-
*
|
27
|
+
* Thin
|
28
28
|
* Redis
|
29
29
|
* Sinatra
|
30
30
|
|
@@ -57,10 +57,39 @@ You can publish to channels using the `Sinatra::PubSub.publish_all(msg)` or
|
|
57
57
|
Messages will be automatically serialized to JSON - you'll need to parse them on the client.
|
58
58
|
See the `./examples` dir for more use-cases.
|
59
59
|
|
60
|
-
##
|
60
|
+
## Herkou 123
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
It's often a good idea to create a separate streaming server on a subdomain, rather than
|
63
|
+
using your main application server for streaming. This gives you a bit more flexibility, like
|
64
|
+
letting you use a Unicorn for your application server, and Thin for your streaming server.
|
65
|
+
This is a quick guide to creating exactly that on Heroku.
|
66
|
+
|
67
|
+
First create your streaming server from the 'basic' example.
|
68
|
+
|
69
|
+
git clone https://github.com/maccman/sinatra-pubsub.git
|
70
|
+
cp -r sinatra-pubsub/examples/basic myapp-stream
|
71
|
+
cd myapp-stream
|
72
|
+
bundle install
|
73
|
+
heroku create myapp-stream
|
74
|
+
git push heroku master
|
75
|
+
|
76
|
+
Now the server is deployed, you need to setup Redis. If you already have Redis running
|
77
|
+
on your main application server, just run:
|
78
|
+
|
79
|
+
heroku config -a myapp | grep REDIS
|
80
|
+
heroku config:set REDIS_URL=YOUR_REDIS_URL -a myapp-stream
|
81
|
+
|
82
|
+
Otherwise install the Redis To Go addon:
|
83
|
+
|
84
|
+
heroku addons:add redis:basic -a myapp-stream
|
85
|
+
|
86
|
+
And you're finished! You can subscribe to a channel from any browser like this:
|
87
|
+
|
88
|
+
var es = new EventSource('http://myapp-stream.herokuapp.com/subscribe');
|
89
|
+
es.onmessage = function(e) {
|
90
|
+
console.log(JSON.parse(e.data));
|
91
|
+
};
|
92
|
+
|
93
|
+
And publish to the stream from any Ruby client connected to the same Redis server:
|
94
|
+
|
95
|
+
Sinatra::PubSub.publish_all('Hello World')
|
File without changes
|
File without changes
|
File without changes
|
@@ -1,11 +1,17 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
Bundler.require
|
3
5
|
|
4
6
|
register Sinatra::PubSub
|
5
7
|
|
8
|
+
Sinatra::PubSub.set(
|
9
|
+
cors: false
|
10
|
+
)
|
11
|
+
|
6
12
|
EventMachine.next_tick do
|
7
13
|
EventMachine::PeriodicTimer.new(1) do
|
8
|
-
Sinatra::PubSub.
|
14
|
+
Sinatra::PubSub.publish('tick', type: 'tick')
|
9
15
|
end
|
10
16
|
end
|
11
17
|
|
@@ -21,6 +27,6 @@ __END__
|
|
21
27
|
|
22
28
|
<script>
|
23
29
|
// reading
|
24
|
-
var es = new EventSource('/subscribe');
|
30
|
+
var es = new EventSource('/subscribe/tick');
|
25
31
|
es.onmessage = function(e) { log.innerHTML += "\n" + e.data };
|
26
32
|
</script>
|
data/lib/sinatra/pubsub/app.rb
CHANGED
@@ -4,6 +4,7 @@ module Sinatra
|
|
4
4
|
configure do
|
5
5
|
set :cors, true
|
6
6
|
set :origin, '*'
|
7
|
+
set :server, 'thin'
|
7
8
|
end
|
8
9
|
|
9
10
|
helpers do
|
@@ -19,7 +20,7 @@ module Sinatra
|
|
19
20
|
secure = origin =~ /\Ahttps/
|
20
21
|
|
21
22
|
allow = settings.origin
|
22
|
-
allow.gsub
|
23
|
+
allow = allow.gsub(/\Ahttp:\/\//, 'https://') if secure
|
23
24
|
|
24
25
|
headers['Access-Control-Allow-Origin'] = allow
|
25
26
|
headers['Access-Control-Allow-Methods'] = 'GET'
|
@@ -30,7 +31,7 @@ module Sinatra
|
|
30
31
|
error 402 unless Stream.enabled?
|
31
32
|
|
32
33
|
stream :keep_open do |out|
|
33
|
-
stream
|
34
|
+
stream = Stream.new(out)
|
34
35
|
|
35
36
|
if channels = params[:splat]
|
36
37
|
stream.subscribe(channels.join('/'))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-pubsub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex MacCaw
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,10 +92,14 @@ files:
|
|
92
92
|
- LICENSE.txt
|
93
93
|
- README.md
|
94
94
|
- Rakefile
|
95
|
-
- examples/
|
96
|
-
- examples/
|
97
|
-
- examples/
|
98
|
-
- examples/
|
95
|
+
- examples/basic/Gemfile
|
96
|
+
- examples/basic/app.rb
|
97
|
+
- examples/basic/config.ru
|
98
|
+
- examples/frontend/events.module.coffee
|
99
|
+
- examples/frontend/stream.module.coffee
|
100
|
+
- examples/tick/Gemfile
|
101
|
+
- examples/tick/app.rb
|
102
|
+
- examples/tick/config.ru
|
99
103
|
- lib/sinatra/pubsub.rb
|
100
104
|
- lib/sinatra/pubsub/app.rb
|
101
105
|
- lib/sinatra/pubsub/helpers.rb
|