sse-rails-engine 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +0 -5
- data/config/initializers/sse_rails_engine.rb +1 -1
- data/lib/sse_rails_engine/manager.rb +20 -6
- data/lib/sse_rails_engine/version.rb +1 -1
- data/test/dummy/log/test.log +74 -0
- data/test/lib/{sse_rails_engine_test.rb → sse-rails-engine_test.rb} +5 -0
- data/test/lib/sse_rails_engine/manager_test.rb +10 -9
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 215385adafbeb44e44ff64603e5e4777206f100a
|
4
|
+
data.tar.gz: 18f56fa8185eaa46cc67ef115d0123ee895d3cd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57a701b21132ca315e60e79c93527eb1d35d4276b45f2c79059b3209909e360f43fdd9e699f1ac94fd2cfadb252c2c335b1f837c81fbd98661705df26e12456f
|
7
|
+
data.tar.gz: 5651157dba6581bb886589cb7f9b48cc116982c54d05427ae46e85d85eb42b0915ac1ac8b7a854588722fcce14d38690ab379aac139c16029e55b4cc6729a7bd
|
data/Rakefile
CHANGED
@@ -14,12 +14,8 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
14
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
17
|
load 'rails/tasks/statistics.rake'
|
20
18
|
|
21
|
-
|
22
|
-
|
23
19
|
Bundler::GemHelper.install_tasks
|
24
20
|
|
25
21
|
require 'rake/testtask'
|
@@ -31,5 +27,4 @@ Rake::TestTask.new(:test) do |t|
|
|
31
27
|
t.verbose = false
|
32
28
|
end
|
33
29
|
|
34
|
-
|
35
30
|
task default: :test
|
@@ -1,2 +1,2 @@
|
|
1
1
|
|
2
|
-
Rails.configuration.middleware.delete
|
2
|
+
Rails.configuration.middleware.delete 'Rack::Lock'
|
@@ -1,14 +1,26 @@
|
|
1
1
|
module SseRailsEngine
|
2
|
+
# This class provides the ability to track SSE connections and broadcast events
|
3
|
+
# to all connected clients from anywhere in your Rails app.
|
4
|
+
#
|
5
|
+
# Example Usage:
|
6
|
+
#
|
7
|
+
# class MyController < ActionController::Base
|
8
|
+
# def do_stuff
|
9
|
+
# SseRailsEngine.send_event('event name', 'any ruby object or string for data')
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# Note: SSEs are not currently supported by IE.
|
2
14
|
class Manager
|
3
15
|
RackHijackUnsupported = Class.new RuntimeError
|
4
16
|
|
5
17
|
attr_reader :connections, :heartbeat_thread
|
6
18
|
|
7
19
|
SSE_HEADER = ["HTTP/1.1 200 OK\r\n",
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
20
|
+
"Content-Type: text/event-stream\r\n",
|
21
|
+
"Cache-Control: no-cache, no-store\r\n",
|
22
|
+
"Connection: close\r\n",
|
23
|
+
"\r\n"].join.freeze
|
12
24
|
|
13
25
|
def initialize
|
14
26
|
@mutex = Mutex.new
|
@@ -23,10 +35,11 @@ module SseRailsEngine
|
|
23
35
|
# Perform full hijack of socket (http://old.blog.phusion.nl/2013/01/23/the-new-rack-socket-hijacking-api/)
|
24
36
|
SseRailsEngine.manager.open_connection(socket)
|
25
37
|
else
|
26
|
-
|
38
|
+
fail RackHijackUnsupported, 'This Rack server does not support hijacking, ensure you are using >= v1.5 of Rack'
|
27
39
|
end
|
28
40
|
end
|
29
41
|
|
42
|
+
# rubocop:disable Metrics/MethodLength
|
30
43
|
def send_event(name, data)
|
31
44
|
@mutex.synchronize do
|
32
45
|
@connections.dup.each do |stream, sse|
|
@@ -43,6 +56,7 @@ module SseRailsEngine
|
|
43
56
|
end
|
44
57
|
end
|
45
58
|
end
|
59
|
+
# rubocop:enable Metrics/MethodLength
|
46
60
|
|
47
61
|
def open_connection(io)
|
48
62
|
Rails.logger.debug "New SSE Client connected: #{io}"
|
@@ -54,7 +68,7 @@ module SseRailsEngine
|
|
54
68
|
|
55
69
|
def call(env)
|
56
70
|
SseRailsEngine.manager.register(env)
|
57
|
-
[
|
71
|
+
[-1, {}, []]
|
58
72
|
end
|
59
73
|
|
60
74
|
private
|
data/test/dummy/log/test.log
CHANGED
@@ -71,3 +71,77 @@ Starting SSE heartbeat thread!
|
|
71
71
|
New SSE Client connected: #<StringIO:0x007ff339278948>
|
72
72
|
Starting SSE heartbeat thread!
|
73
73
|
Starting SSE heartbeat thread!
|
74
|
+
Starting SSE heartbeat thread!
|
75
|
+
Starting SSE heartbeat thread!
|
76
|
+
New SSE Client connected: #<StringIO:0x007fcdfb259ec0>
|
77
|
+
Starting SSE heartbeat thread!
|
78
|
+
Starting SSE heartbeat thread!
|
79
|
+
New SSE Client connected: #<StringIO:0x007fcdfb230ef8>
|
80
|
+
Starting SSE heartbeat thread!
|
81
|
+
Starting SSE heartbeat thread!
|
82
|
+
New SSE Client connected: #<StringIO:0x007fcdfb21bc88>
|
83
|
+
Failed to send event to SSE: #<StringIO:0x007fcdfb21bc88> (foo, bar - RuntimeError (RuntimeError
|
84
|
+
Starting SSE heartbeat thread!
|
85
|
+
Starting SSE heartbeat thread!
|
86
|
+
New SSE Client connected: #<StringIO:0x007fcdfb2035c0>
|
87
|
+
SSE Client disconnected: #<StringIO:0x007fcdfb2035c0>
|
88
|
+
Starting SSE heartbeat thread!
|
89
|
+
New SSE Client connected: #<StringIO:0x007fcdfb200938>
|
90
|
+
Starting SSE heartbeat thread!
|
91
|
+
New SSE Client connected: #<StringIO:0x007fcdfb1fa290>
|
92
|
+
Starting SSE heartbeat thread!
|
93
|
+
Starting SSE heartbeat thread!
|
94
|
+
Starting SSE heartbeat thread!
|
95
|
+
Starting SSE heartbeat thread!
|
96
|
+
New SSE Client connected: #<StringIO:0x007fa81cd396b0>
|
97
|
+
Failed to send event to SSE: #<StringIO:0x007fa81cd396b0> (foo, bar - RuntimeError (RuntimeError
|
98
|
+
Starting SSE heartbeat thread!
|
99
|
+
Starting SSE heartbeat thread!
|
100
|
+
New SSE Client connected: #<StringIO:0x007fa81cd33288>
|
101
|
+
Starting SSE heartbeat thread!
|
102
|
+
New SSE Client connected: #<StringIO:0x007fa81cd23b58>
|
103
|
+
Starting SSE heartbeat thread!
|
104
|
+
New SSE Client connected: #<StringIO:0x007fa81cd21038>
|
105
|
+
Starting SSE heartbeat thread!
|
106
|
+
Starting SSE heartbeat thread!
|
107
|
+
New SSE Client connected: #<StringIO:0x007fa81cd13c80>
|
108
|
+
SSE Client disconnected: #<StringIO:0x007fa81cd13c80>
|
109
|
+
Starting SSE heartbeat thread!
|
110
|
+
New SSE Client connected: #<StringIO:0x007fa81cd10cd8>
|
111
|
+
Starting SSE heartbeat thread!
|
112
|
+
Starting SSE heartbeat thread!
|
113
|
+
New SSE Client connected: #<StringIO:0x007fd37d510920>
|
114
|
+
Failed to send event to SSE: #<StringIO:0x007fd37d510920> (foo, bar - RuntimeError (RuntimeError
|
115
|
+
Starting SSE heartbeat thread!
|
116
|
+
New SSE Client connected: #<StringIO:0x007fd37d4f9a40>
|
117
|
+
Starting SSE heartbeat thread!
|
118
|
+
Starting SSE heartbeat thread!
|
119
|
+
Starting SSE heartbeat thread!
|
120
|
+
New SSE Client connected: #<StringIO:0x007fd37d4d8368>
|
121
|
+
Starting SSE heartbeat thread!
|
122
|
+
New SSE Client connected: #<StringIO:0x007fd37d4c2a68>
|
123
|
+
SSE Client disconnected: #<StringIO:0x007fd37d4c2a68>
|
124
|
+
Starting SSE heartbeat thread!
|
125
|
+
New SSE Client connected: #<StringIO:0x007fd37d4bb9c0>
|
126
|
+
Starting SSE heartbeat thread!
|
127
|
+
New SSE Client connected: #<StringIO:0x007fd37d4b95f8>
|
128
|
+
Starting SSE heartbeat thread!
|
129
|
+
Starting SSE heartbeat thread!
|
130
|
+
Starting SSE heartbeat thread!
|
131
|
+
Starting SSE heartbeat thread!
|
132
|
+
New SSE Client connected: #<StringIO:0x007ff76cdeae70>
|
133
|
+
SSE Client disconnected: #<StringIO:0x007ff76cdeae70>
|
134
|
+
Starting SSE heartbeat thread!
|
135
|
+
New SSE Client connected: #<StringIO:0x007ff76cde3f30>
|
136
|
+
Starting SSE heartbeat thread!
|
137
|
+
Starting SSE heartbeat thread!
|
138
|
+
Starting SSE heartbeat thread!
|
139
|
+
New SSE Client connected: #<StringIO:0x007ff76cdcb728>
|
140
|
+
Starting SSE heartbeat thread!
|
141
|
+
Starting SSE heartbeat thread!
|
142
|
+
New SSE Client connected: #<StringIO:0x007ff76cdb2fe8>
|
143
|
+
Starting SSE heartbeat thread!
|
144
|
+
New SSE Client connected: #<StringIO:0x007ff76cdb0838>
|
145
|
+
Starting SSE heartbeat thread!
|
146
|
+
New SSE Client connected: #<StringIO:0x007ff76cda0118>
|
147
|
+
Failed to send event to SSE: #<StringIO:0x007ff76cda0118> (foo, bar - RuntimeError (RuntimeError
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require_relative '../../test_helper'
|
2
2
|
|
3
3
|
describe SseRailsEngine::Manager do
|
4
|
-
let(:manager) { SseRailsEngine
|
5
|
-
let(:env)
|
4
|
+
let(:manager) { SseRailsEngine.manager }
|
5
|
+
let(:env) do
|
6
6
|
Hashie::Mash.new('rack.hijack?' => true,
|
7
|
-
'rack.hijack' => ->() {
|
7
|
+
'rack.hijack' => ->() {},
|
8
8
|
'rack.hijack_io' => StringIO.new)
|
9
|
-
|
9
|
+
end
|
10
10
|
|
11
11
|
before do
|
12
12
|
SseRailsEngine.instance_variable_set(:@manager, nil)
|
@@ -54,19 +54,20 @@ describe SseRailsEngine::Manager do
|
|
54
54
|
|
55
55
|
it 'writes event object to stream' do
|
56
56
|
manager.register(env)
|
57
|
-
manager.send_event('foo',
|
58
|
-
env['rack.hijack_io'].string.must_equal(
|
57
|
+
manager.send_event('foo', a: 123, 'b' => 'abc', c: { foo: 'bar' })
|
58
|
+
env['rack.hijack_io'].string.must_equal(
|
59
|
+
SseRailsEngine::Manager::SSE_HEADER + "event: foo\ndata: {\"a\":123,\"b\":\"abc\",\"c\":{\"foo\":\"bar\"}}\n\n")
|
59
60
|
end
|
60
61
|
|
61
62
|
it 'writes minimum headers to rack middleware' do
|
62
|
-
manager.call(env).must_equal [
|
63
|
+
manager.call(env).must_equal [-1, {}, []]
|
63
64
|
end
|
64
65
|
|
65
66
|
it 'ensures heartbeat is sent' do
|
66
|
-
SseRailsEngine.stubs(:heartbeat_interval).returns(0,5)
|
67
|
+
SseRailsEngine.stubs(:heartbeat_interval).returns(0, 5)
|
67
68
|
SseRailsEngine::Manager.unstub(:start_heartbeats)
|
68
69
|
SseRailsEngine::Manager.any_instance.expects(:send_event).once
|
69
|
-
|
70
|
+
SseRailsEngine::Manager.new
|
70
71
|
sleep 0.2
|
71
72
|
end
|
72
73
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sse-rails-engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Hender
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: See the README.md at https://github.com/henders/sse-rails-engine
|
84
98
|
email:
|
85
99
|
- henders@gmail.com
|
@@ -129,8 +143,8 @@ files:
|
|
129
143
|
- test/dummy/public/422.html
|
130
144
|
- test/dummy/public/500.html
|
131
145
|
- test/dummy/public/favicon.ico
|
146
|
+
- test/lib/sse-rails-engine_test.rb
|
132
147
|
- test/lib/sse_rails_engine/manager_test.rb
|
133
|
-
- test/lib/sse_rails_engine_test.rb
|
134
148
|
- test/test_helper.rb
|
135
149
|
homepage: https://github.com/henders/sse-rails-engine
|
136
150
|
licenses:
|
@@ -190,6 +204,6 @@ test_files:
|
|
190
204
|
- test/dummy/public/500.html
|
191
205
|
- test/dummy/public/favicon.ico
|
192
206
|
- test/dummy/Rakefile
|
207
|
+
- test/lib/sse-rails-engine_test.rb
|
193
208
|
- test/lib/sse_rails_engine/manager_test.rb
|
194
|
-
- test/lib/sse_rails_engine_test.rb
|
195
209
|
- test/test_helper.rb
|