r4s 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +1 -0
- data/lib/r4s/version.rb +3 -0
- data/lib/r4s.rb +65 -0
- data/r4s.gemspec +20 -0
- metadata +53 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# R4S (Rails 4 Streaming)
|
2
|
+
|
3
|
+
R4S is a gem that simplifies sending server side events (SSE) to multiple browsers in Rails 4.
|
4
|
+
It is supposed to simulate broadcasting to all the browsers that are connected to it.
|
5
|
+
|
6
|
+
Here is a ascii picture that shows how a webpage would send a 'update' event to three browsers.
|
7
|
+
|
8
|
+
My ASCII Picture. :)
|
9
|
+
|
10
|
+
|---------| /-----------> browser0
|
11
|
+
| Webpage | --['update']--------|------------> browser1
|
12
|
+
|---------| \-----------> browser2
|
13
|
+
|
14
|
+
|
15
|
+
Most of my code is based on Tenderloves "Is it live?" article. http://tenderlovemaking.com/2012/07/30/is-it-live.html
|
16
|
+
|
17
|
+
## NOTE
|
18
|
+
This gem works only in Rails 4 (I thinks)
|
19
|
+
|
20
|
+
(I think I have found out what's wrong with turbolinks and r4s. Fix should come soon)
|
21
|
+
You can not use turbolinks with this project for some strange, inapparent reason.
|
22
|
+
If you make a connection and then close the browser the srv will crash stating that the headers had already been sent.
|
23
|
+
who, why, where or how these headers are sent are a mistery to me. My solution was to remove the turbolinks.
|
24
|
+
This might be fixed in the future.
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
gem 'r4s'
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle install
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
You'll need a controller that has included `ActionController::Live` to be able to stream.
|
39
|
+
|
40
|
+
A page with javascript EventSource to connect to the stream.
|
41
|
+
|
42
|
+
And some controller.action to trigger the stream.event.
|
43
|
+
|
44
|
+
R4S uses only two functions
|
45
|
+
|
46
|
+
One to create the stream(s)
|
47
|
+
|
48
|
+
R4S.add_stream(response,"key").start
|
49
|
+
|
50
|
+
One to push to the stream(s)
|
51
|
+
|
52
|
+
R4S.push_data("key",data,options)
|
53
|
+
|
54
|
+
## Example
|
55
|
+
I created a "stream" controller to handle my streams
|
56
|
+
|
57
|
+
class StreamController < ApplicationController
|
58
|
+
include ActionController::Live
|
59
|
+
def stream1
|
60
|
+
R4S.add_stream(response,"stream.key").start
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Create a webpage that connects to the stream.
|
65
|
+
`app/views/home/show.html.erb`
|
66
|
+
|
67
|
+
<h1>Event Page</h1>
|
68
|
+
<p>The time : <span id="time"></span></p>
|
69
|
+
<script>
|
70
|
+
jQuery(document).ready(function() {
|
71
|
+
source = new EventSource('/stream/stream1');
|
72
|
+
source.addEventListener('refresh', function(e) {
|
73
|
+
var data = JSON.parse(e.data);
|
74
|
+
jQuery('#time').html(data["time"]);
|
75
|
+
});
|
76
|
+
});
|
77
|
+
</script>
|
78
|
+
|
79
|
+
Create a action that triggers the event.
|
80
|
+
|
81
|
+
class HomeController < ApplicationController
|
82
|
+
def index
|
83
|
+
R4S.push_data('kanban',{:time=>Time.now},:event=>"refresh")
|
84
|
+
render :json => {:success=>true}
|
85
|
+
end
|
86
|
+
|
87
|
+
def show
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
1. Fork it
|
95
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
96
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
97
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
98
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/r4s/version.rb
ADDED
data/lib/r4s.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
class R4S
|
2
|
+
SSES = {}
|
3
|
+
|
4
|
+
def self.add_stream(response,key="none")
|
5
|
+
|
6
|
+
if !R4S::SSES.keys.include?(key)
|
7
|
+
R4S::SSES[key] = {}
|
8
|
+
key_count = 0
|
9
|
+
else
|
10
|
+
key_count = R4S::SSES[key].keys.count
|
11
|
+
end
|
12
|
+
|
13
|
+
sse = R4S::SSE.new(response,key,key_count)
|
14
|
+
R4S::SSES[key][key_count] = sse
|
15
|
+
|
16
|
+
return sse
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.push_data(key,data,options={})
|
21
|
+
if R4S::SSES.keys.include?(key)
|
22
|
+
R4S::SSES[key].each do |k,sse|
|
23
|
+
sse.write(data,options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class SSE
|
29
|
+
|
30
|
+
def initialize response, key, id
|
31
|
+
response.headers['Content-Type'] = 'text/event-stream'
|
32
|
+
@response = response
|
33
|
+
@id = id
|
34
|
+
@key = key
|
35
|
+
@io = response.stream
|
36
|
+
end
|
37
|
+
|
38
|
+
def write object, options = {}
|
39
|
+
options.each do |k,v|
|
40
|
+
@io.write "#{k}: #{v}\n"
|
41
|
+
end
|
42
|
+
@io.write "data: #{JSON.dump(object)}\n\n"
|
43
|
+
end
|
44
|
+
|
45
|
+
def start
|
46
|
+
begin
|
47
|
+
while !@io.closed?;
|
48
|
+
sleep 30
|
49
|
+
end
|
50
|
+
rescue
|
51
|
+
ensure
|
52
|
+
R4S::SSES[@key][@id].close
|
53
|
+
R4S::SSES[@key].delete(@id)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def closed?
|
58
|
+
@io.closed?
|
59
|
+
end
|
60
|
+
|
61
|
+
def close
|
62
|
+
@io.close
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/r4s.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'r4s/version'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = "r4s"
|
9
|
+
gem.version = R4s::VERSION
|
10
|
+
gem.authors = ["Birgir Hrafn Sigurðsson"]
|
11
|
+
gem.email = ["biggihs@gmail.com"]
|
12
|
+
gem.description = %q{Wrapper for Rails 4 streaming}
|
13
|
+
gem.summary = %q{Wrapper for Rails 4 streaming}
|
14
|
+
gem.homepage = ""
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: r4s
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Birgir Hrafn Sigurðsson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Wrapper for Rails 4 streaming
|
15
|
+
email:
|
16
|
+
- biggihs@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/r4s.rb
|
27
|
+
- lib/r4s/version.rb
|
28
|
+
- r4s.gemspec
|
29
|
+
homepage: ''
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.24
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Wrapper for Rails 4 streaming
|
53
|
+
test_files: []
|