sse-rails-engine 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 215385adafbeb44e44ff64603e5e4777206f100a
4
- data.tar.gz: 18f56fa8185eaa46cc67ef115d0123ee895d3cd7
3
+ metadata.gz: 0bab1f8bb9471deacf8e724b2763a6f21c20f350
4
+ data.tar.gz: ea78faac1bb777ce07993f90043358edd9b1a57d
5
5
  SHA512:
6
- metadata.gz: 57a701b21132ca315e60e79c93527eb1d35d4276b45f2c79059b3209909e360f43fdd9e699f1ac94fd2cfadb252c2c335b1f837c81fbd98661705df26e12456f
7
- data.tar.gz: 5651157dba6581bb886589cb7f9b48cc116982c54d05427ae46e85d85eb42b0915ac1ac8b7a854588722fcce14d38690ab379aac139c16029e55b4cc6729a7bd
6
+ metadata.gz: 4d67d7f168f7d0cbd71ecf18f4311e48c56e5c0c7fa1e7303076321da994e0049a2a2472be12d15a31d395309119a032a061b88cbb91a55c2787535ff69bc518
7
+ data.tar.gz: f9a240475088861184a371d25441371f4a35bc7c606222bae0ddac50027746556f9661ce9f21fc5ca4008200f8471912d9e0a3a69a527aa3f0c90f0f5b72343d
data/README.md CHANGED
@@ -47,6 +47,38 @@ Then you can send an event from anywhere in your app:
47
47
  SseRailsEngine.send_event('test', { foo: 'bar' })
48
48
  ```
49
49
 
50
+ # Channels
51
+
52
+ It supports the idea of channels so that if pages don't need to receive certain events, then you won't waste
53
+ the bandwidth or processing on the server:
54
+
55
+ Clientside:
56
+ ```
57
+ $(document).ready(function () {
58
+ var source = new EventSource('/sse?channels=foo,bar,baz');
59
+
60
+ source.addEventListener('other', function(e) {
61
+ // Will never be called
62
+ }, true);
63
+
64
+ source.addEventListener('foo', function(e) {
65
+ // Do stuff
66
+ }, true);
67
+ });
68
+
69
+ ```
70
+ Serverside:
71
+ ```
72
+ SseRailsEngine.send_event('foo', '') # Sent to the client
73
+ SseRailsEngine.send_event('other', '') # Won't be sent to the client
74
+ ```
75
+
76
+ # Notes
77
+
78
+ ## Nginx
79
+ You may need to configure nginx to not buffer or cache SSE connections:
80
+ http://stackoverflow.com/questions/13672743/eventsource-server-sent-events-through-nginx
81
+
50
82
  # License
51
83
 
52
84
  MIT - Have at it! :)
@@ -1,4 +1,5 @@
1
1
  require 'sse_rails_engine/engine'
2
+ require 'sse_rails_engine/connection'
2
3
  require 'sse_rails_engine/manager'
3
4
 
4
5
  module SseRailsEngine
@@ -0,0 +1,36 @@
1
+ module SseRailsEngine
2
+ class Connection
3
+ attr_accessor :stream
4
+
5
+ SSE_HEADER = ["HTTP/1.1 200 OK\r\n",
6
+ "Content-Type: text/event-stream\r\n",
7
+ "Cache-Control: no-cache, no-store\r\n",
8
+ "Connection: close\r\n",
9
+ "\r\n"].join.freeze
10
+
11
+ def initialize(io, env)
12
+ @socket = io
13
+ @socket.write SSE_HEADER
14
+ @socket.flush
15
+ @stream = ActionController::Live::SSE.new(io)
16
+ @channels = requested_channels(env)
17
+ end
18
+
19
+ def write(name, data)
20
+ return if filtered?(name)
21
+ @stream.write(data, event: name)
22
+ @socket.flush
23
+ end
24
+
25
+ private
26
+
27
+ def filtered?(channel)
28
+ return false if @channels.empty? || channel == Manager::HEARTBEAT_EVENT
29
+ !@channels.include? channel
30
+ end
31
+
32
+ def requested_channels(env)
33
+ Rack::Utils.parse_query(env['QUERY_STRING']).fetch('channels', []).split(',').flatten
34
+ end
35
+ end
36
+ end
@@ -16,11 +16,7 @@ module SseRailsEngine
16
16
 
17
17
  attr_reader :connections, :heartbeat_thread
18
18
 
19
- SSE_HEADER = ["HTTP/1.1 200 OK\r\n",
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
19
+ HEARTBEAT_EVENT = 'heartbeat'
24
20
 
25
21
  def initialize
26
22
  @mutex = Mutex.new
@@ -33,41 +29,34 @@ module SseRailsEngine
33
29
  env['rack.hijack'].call
34
30
  socket = env['rack.hijack_io']
35
31
  # Perform full hijack of socket (http://old.blog.phusion.nl/2013/01/23/the-new-rack-socket-hijacking-api/)
36
- SseRailsEngine.manager.open_connection(socket)
32
+ open_connection(socket, env)
37
33
  else
38
34
  fail RackHijackUnsupported, 'This Rack server does not support hijacking, ensure you are using >= v1.5 of Rack'
39
35
  end
40
36
  end
41
37
 
42
- # rubocop:disable Metrics/MethodLength
43
- def send_event(name, data)
38
+ def send_event(name, data = '')
44
39
  @mutex.synchronize do
45
- @connections.dup.each do |stream, sse|
40
+ @connections.dup.each do |stream, connection|
46
41
  begin
47
- sse.write(data, event: name)
48
- stream.flush
49
- rescue IOError, Errno::EPIPE, Errno::ETIMEDOUT
50
- Rails.logger.debug "SSE Client disconnected: #{stream}"
51
- close_connection(stream)
52
- rescue => ex
53
- Rails.logger.error "Failed to send event to SSE: #{stream} (#{name}, #{data} - #{ex.message} (#{ex.class}"
42
+ connection.write(name, data)
43
+ rescue IOError, Errno::EPIPE, Errno::ETIMEDOUT => ex
44
+ Rails.logger.debug "SSE Client disconnected: #{stream} - #{ex.message}"
54
45
  close_connection(stream)
55
46
  end
56
47
  end
57
48
  end
58
49
  end
59
- # rubocop:enable Metrics/MethodLength
60
50
 
61
- def open_connection(io)
51
+ def open_connection(io, env)
62
52
  Rails.logger.debug "New SSE Client connected: #{io}"
63
- io.write(SSE_HEADER)
64
53
  @mutex.synchronize do
65
- @connections[io] = ActionController::Live::SSE.new(io)
54
+ @connections[io] = Connection.new(io, env)
66
55
  end
67
56
  end
68
57
 
69
58
  def call(env)
70
- SseRailsEngine.manager.register(env)
59
+ register(env)
71
60
  [-1, {}, []]
72
61
  end
73
62
 
@@ -75,7 +64,7 @@ module SseRailsEngine
75
64
 
76
65
  def close_connection(stream)
77
66
  return if @connections[stream].nil?
78
- @connections[stream].close
67
+ @connections[stream].stream.close
79
68
  @connections.delete(stream)
80
69
  end
81
70
 
@@ -84,7 +73,7 @@ module SseRailsEngine
84
73
  @heartbeat_thread = Thread.new do
85
74
  loop do
86
75
  sleep SseRailsEngine.heartbeat_interval
87
- send_event('heartbeat', '')
76
+ send_event(HEARTBEAT_EVENT)
88
77
  end
89
78
  end
90
79
  end
@@ -1,3 +1,3 @@
1
1
  module SseRailsEngine
2
- VERSION = '1.2.2'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -145,3 +145,422 @@ New SSE Client connected: #<StringIO:0x007ff76cdb0838>
145
145
  Starting SSE heartbeat thread!
146
146
  New SSE Client connected: #<StringIO:0x007ff76cda0118>
147
147
  Failed to send event to SSE: #<StringIO:0x007ff76cda0118> (foo, bar - RuntimeError (RuntimeError
148
+ Starting SSE heartbeat thread!
149
+ Starting SSE heartbeat thread!
150
+ New SSE Client connected: #<StringIO:0x007f8ae344fe88>
151
+ New SSE Client connected: #<StringIO:0x007f8ae345c5c0>
152
+ Starting SSE heartbeat thread!
153
+ Starting SSE heartbeat thread!
154
+ New SSE Client connected: #<StringIO:0x007f98fbe791b8>
155
+ New SSE Client connected: #<StringIO:0x007f98fbe797f8>
156
+ Starting SSE heartbeat thread!
157
+ Starting SSE heartbeat thread!
158
+ New SSE Client connected: #<StringIO:0x007fa5842f1f80>
159
+ New SSE Client connected: #<StringIO:0x007fa5842f2480>
160
+ Starting SSE heartbeat thread!
161
+ Starting SSE heartbeat thread!
162
+ New SSE Client connected: #<StringIO:0x007ffe6c069308>
163
+ New SSE Client connected: #<StringIO:0x007ffe6c06a618>
164
+ Starting SSE heartbeat thread!
165
+ Starting SSE heartbeat thread!
166
+ New SSE Client connected: #<StringIO:0x007ffc85471538>
167
+ New SSE Client connected: #<StringIO:0x007ffc85471e48>
168
+ Starting SSE heartbeat thread!
169
+ Starting SSE heartbeat thread!
170
+ New SSE Client connected: #<StringIO:0x007fe576fe1330>
171
+ New SSE Client connected: #<StringIO:0x007fe576fe1d30>
172
+ Starting SSE heartbeat thread!
173
+ Starting SSE heartbeat thread!
174
+ New SSE Client connected: #<StringIO:0x007f80c45c2cd0>
175
+ New SSE Client connected: #<StringIO:0x007f80c45c36a8>
176
+ Starting SSE heartbeat thread!
177
+ Starting SSE heartbeat thread!
178
+ New SSE Client connected: #<StringIO:0x007fb0cde30c80>
179
+ New SSE Client connected: #<StringIO:0x007fb0cde314f0>
180
+ Starting SSE heartbeat thread!
181
+ Starting SSE heartbeat thread!
182
+ New SSE Client connected: #<StringIO:0x007fb95c5c9758>
183
+ New SSE Client connected: #<StringIO:0x007fb95c5c88a8>
184
+ Starting SSE heartbeat thread!
185
+ Starting SSE heartbeat thread!
186
+ New SSE Client connected: #<StringIO:0x007fb3c8f8e5c0>
187
+ New SSE Client connected: #<StringIO:0x007fb3c8f8ed90>
188
+ Starting SSE heartbeat thread!
189
+ Starting SSE heartbeat thread!
190
+ New SSE Client connected: #<StringIO:0x007f95fcc63ce0>
191
+ New SSE Client connected: #<StringIO:0x007f95fcc70670>
192
+ Starting SSE heartbeat thread!
193
+ Starting SSE heartbeat thread!
194
+ New SSE Client connected: #<StringIO:0x007fc68bd72090>
195
+ Failed to send event to SSE: #<StringIO:0x007fc68bd72090> (foo, bar - undefined method `channels' for #<Hash:0x007fc68bd70e98> (NoMethodError
196
+ Starting SSE heartbeat thread!
197
+ New SSE Client connected: #<StringIO:0x007fc68bd696c0>
198
+ Starting SSE heartbeat thread!
199
+ Starting SSE heartbeat thread!
200
+ New SSE Client connected: #<StringIO:0x007fc68bd5a4e0>
201
+ Failed to send event to SSE: #<StringIO:0x007fc68bd5a4e0> (foo, bar - undefined method `channels' for #<Hash:0x007fc68bd59e00> (NoMethodError
202
+ Starting SSE heartbeat thread!
203
+ Starting SSE heartbeat thread!
204
+ Starting SSE heartbeat thread!
205
+ New SSE Client connected: #<StringIO:0x007fc68bd2b028>
206
+ Failed to send event to SSE: #<StringIO:0x007fc68bd2b028> (foo, bar - undefined method `channels' for #<Hash:0x007fc68bd2a7e0> (NoMethodError
207
+ Starting SSE heartbeat thread!
208
+ New SSE Client connected: #<StringIO:0x007fc68bd12f00>
209
+ New SSE Client connected: #<StringIO:0x007fc68bd13518>
210
+ Starting SSE heartbeat thread!
211
+ ENV:
212
+ New SSE Client connected: #<StringIO:0x007fc68bd027b8>
213
+ Starting SSE heartbeat thread!
214
+ New SSE Client connected: #<StringIO:0x007fc68bd001e8>
215
+ Failed to send event to SSE: #<StringIO:0x007fc68bd001e8> (foo, {:a=>123, "b"=>"abc", :c=>{:foo=>"bar"}} - undefined method `channels' for #<Hash:0x007fc68bcfbd00> (NoMethodError
216
+ Starting SSE heartbeat thread!
217
+ Starting SSE heartbeat thread!
218
+ New SSE Client connected: #<StringIO:0x007f8f6e5818f8>
219
+ Starting SSE heartbeat thread!
220
+ Starting SSE heartbeat thread!
221
+ New SSE Client connected: #<StringIO:0x007f9abb84bb58>
222
+ Starting SSE heartbeat thread!
223
+ Starting SSE heartbeat thread!
224
+ New SSE Client connected: #<StringIO:0x007fe4a5373890>
225
+ Starting SSE heartbeat thread!
226
+ Starting SSE heartbeat thread!
227
+ New SSE Client connected: #<StringIO:0x007fcffa778590>
228
+ New SSE Client connected: #<StringIO:0x007fcffa778dd8>
229
+ Starting SSE heartbeat thread!
230
+ Starting SSE heartbeat thread!
231
+ New SSE Client connected: #<StringIO:0x007ff97c572ec0>
232
+ New SSE Client connected: #<StringIO:0x007ff97c573690>
233
+ Starting SSE heartbeat thread!
234
+ Starting SSE heartbeat thread!
235
+ New SSE Client connected: #<StringIO:0x007fc5850f9068>
236
+ Starting SSE heartbeat thread!
237
+ New SSE Client connected: #<StringIO:0x007fc5850f1408>
238
+ Starting SSE heartbeat thread!
239
+ New SSE Client connected: #<StringIO:0x007fc5850d2f30>
240
+ Starting SSE heartbeat thread!
241
+ New SSE Client connected: #<StringIO:0x007fc5850aa648>
242
+ New SSE Client connected: #<StringIO:0x007fc5850aaaf8>
243
+ Starting SSE heartbeat thread!
244
+ New SSE Client connected: #<StringIO:0x007fc5850a1930>
245
+ Starting SSE heartbeat thread!
246
+ New SSE Client connected: #<StringIO:0x007fc5850912b0>
247
+ Starting SSE heartbeat thread!
248
+ Starting SSE heartbeat thread!
249
+ Starting SSE heartbeat thread!
250
+ Starting SSE heartbeat thread!
251
+ New SSE Client connected: #<StringIO:0x007fc585072658>
252
+ Starting SSE heartbeat thread!
253
+ Starting SSE heartbeat thread!
254
+ New SSE Client connected: #<StringIO:0x007f8d6ec10c70>
255
+ New SSE Client connected: #<StringIO:0x007f8d6ec11468>
256
+ Starting SSE heartbeat thread!
257
+ Starting SSE heartbeat thread!
258
+ New SSE Client connected: #<StringIO:0x007f91c60c1d68>
259
+ New SSE Client connected: #<StringIO:0x007f91c60c2560>
260
+ Starting SSE heartbeat thread!
261
+ Starting SSE heartbeat thread!
262
+ Starting SSE heartbeat thread!
263
+ New SSE Client connected: #<StringIO:0x007fb0829b5bd8>
264
+ Starting SSE heartbeat thread!
265
+ New SSE Client connected: #<StringIO:0x007fb0829a64f8>
266
+ Starting SSE heartbeat thread!
267
+ Starting SSE heartbeat thread!
268
+ New SSE Client connected: #<StringIO:0x007fb082955580>
269
+ Starting SSE heartbeat thread!
270
+ Starting SSE heartbeat thread!
271
+ New SSE Client connected: #<StringIO:0x007fb082917988>
272
+ Starting SSE heartbeat thread!
273
+ New SSE Client connected: #<StringIO:0x007fb0828a9140>
274
+ Starting SSE heartbeat thread!
275
+ New SSE Client connected: #<StringIO:0x007fb082861cf0>
276
+ Starting SSE heartbeat thread!
277
+ New SSE Client connected: #<StringIO:0x007fb082841c98>
278
+ New SSE Client connected: #<StringIO:0x007fb082842508>
279
+ Starting SSE heartbeat thread!
280
+ Starting SSE heartbeat thread!
281
+ New SSE Client connected: #<StringIO:0x007fd343f735f0>
282
+ New SSE Client connected: #<StringIO:0x007fd343f73dc0>
283
+ Starting SSE heartbeat thread!
284
+ Starting SSE heartbeat thread!
285
+ New SSE Client connected: #<StringIO:0x007fe58c1a14d0>
286
+ New SSE Client connected: #<StringIO:0x007fe58c1a1b10>
287
+ Starting SSE heartbeat thread!
288
+ Starting SSE heartbeat thread!
289
+ New SSE Client connected: #<StringIO:0x007ff49e6d3f90>
290
+ New SSE Client connected: #<StringIO:0x007ff49e6e0e20>
291
+ SSE Client disconnected: #<StringIO:0x007ff49e6e0e20> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
292
+ Starting SSE heartbeat thread!
293
+ Starting SSE heartbeat thread!
294
+ New SSE Client connected: #<StringIO:0x007f9b99299f28>
295
+ New SSE Client connected: #<StringIO:0x007f9b9929a658>
296
+ SSE Client disconnected: #<StringIO:0x007f9b99299f28> - undefined local variable or method `channel' for #<SseRailsEngine::Connection:0x007f9b99299b18>
297
+ SSE Client disconnected: #<StringIO:0x007f9b9929a658> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
298
+ Starting SSE heartbeat thread!
299
+ Starting SSE heartbeat thread!
300
+ New SSE Client connected: #<StringIO:0x007fa17455ad80>
301
+ New SSE Client connected: #<StringIO:0x007fa17455b5f0>
302
+ SSE Client disconnected: #<StringIO:0x007fa17455b5f0> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
303
+ Starting SSE heartbeat thread!
304
+ Starting SSE heartbeat thread!
305
+ New SSE Client connected: #<StringIO:0x007f9b5d5aa500>
306
+ New SSE Client connected: #<StringIO:0x007f9b5d5aad20>
307
+ SSE Client disconnected: #<StringIO:0x007f9b5d5aad20> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
308
+ Starting SSE heartbeat thread!
309
+ Starting SSE heartbeat thread!
310
+ New SSE Client connected: #<StringIO:0x007f908134f878>
311
+ New SSE Client connected: #<StringIO:0x007f90813740b0>
312
+ SSE Client disconnected: #<StringIO:0x007f90813740b0> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
313
+ Starting SSE heartbeat thread!
314
+ Starting SSE heartbeat thread!
315
+ New SSE Client connected: #<StringIO:0x007f8666022c38>
316
+ Starting SSE heartbeat thread!
317
+ Starting SSE heartbeat thread!
318
+ New SSE Client connected: #<StringIO:0x007f8665f70ee8>
319
+ SSE Client disconnected: #<StringIO:0x007f8665f70ee8> - RuntimeError
320
+ Starting SSE heartbeat thread!
321
+ New SSE Client connected: #<StringIO:0x007f8665f512f0>
322
+ Starting SSE heartbeat thread!
323
+ Starting SSE heartbeat thread!
324
+ New SSE Client connected: #<StringIO:0x007f8665f01200>
325
+ Starting SSE heartbeat thread!
326
+ New SSE Client connected: #<StringIO:0x007f8665ee2a08>
327
+ New SSE Client connected: #<StringIO:0x007f8665ee31d8>
328
+ SSE Client disconnected: #<StringIO:0x007f8665ee31d8> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
329
+ Starting SSE heartbeat thread!
330
+ New SSE Client connected: #<StringIO:0x007f8665ea0b80>
331
+ SSE Client disconnected: #<StringIO:0x007f8665ea0b80> - IOError
332
+ Starting SSE heartbeat thread!
333
+ New SSE Client connected: #<StringIO:0x007f8665e81348>
334
+ Starting SSE heartbeat thread!
335
+ Starting SSE heartbeat thread!
336
+ Starting SSE heartbeat thread!
337
+ New SSE Client connected: #<StringIO:0x007fdbbdb9b360>
338
+ Starting SSE heartbeat thread!
339
+ New SSE Client connected: #<StringIO:0x007fdbbdb7abd8>
340
+ Starting SSE heartbeat thread!
341
+ Starting SSE heartbeat thread!
342
+ New SSE Client connected: #<StringIO:0x007fdbbdb387d8>
343
+ Starting SSE heartbeat thread!
344
+ New SSE Client connected: #<StringIO:0x007fdbbdb0bfa8>
345
+ SSE Client disconnected: #<StringIO:0x007fdbbdb0bfa8> - IOError
346
+ Starting SSE heartbeat thread!
347
+ Starting SSE heartbeat thread!
348
+ New SSE Client connected: #<StringIO:0x007fdbbdadad40>
349
+ Starting SSE heartbeat thread!
350
+ New SSE Client connected: #<StringIO:0x007fdbbdaaa640>
351
+ SSE Client disconnected: #<StringIO:0x007fdbbdaaa640> - RuntimeError
352
+ Starting SSE heartbeat thread!
353
+ Starting SSE heartbeat thread!
354
+ New SSE Client connected: #<StringIO:0x007fdbbda6bdf0>
355
+ New SSE Client connected: #<StringIO:0x007fdbbda784d8>
356
+ SSE Client disconnected: #<StringIO:0x007fdbbda784d8> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
357
+ Starting SSE heartbeat thread!
358
+ Starting SSE heartbeat thread!
359
+ New SSE Client connected: #<StringIO:0x007fb07d2394b8>
360
+ New SSE Client connected: #<StringIO:0x007fb07d23a430>
361
+ SSE Client disconnected: #<StringIO:0x007fb07d23a430> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
362
+ Starting SSE heartbeat thread!
363
+ New SSE Client connected: #<StringIO:0x007fb07d1ea408>
364
+ New SSE Client connected: #<StringIO:0x007fb07d1eab10>
365
+ SSE Client disconnected: #<StringIO:0x007fb07d1eab10> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
366
+ Starting SSE heartbeat thread!
367
+ Starting SSE heartbeat thread!
368
+ New SSE Client connected: #<StringIO:0x007fd5864badf8>
369
+ New SSE Client connected: #<StringIO:0x007fd5864bb410>
370
+ SSE Client disconnected: #<StringIO:0x007fd5864bb410> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
371
+ Starting SSE heartbeat thread!
372
+ New SSE Client connected: #<StringIO:0x007fd58647a6e0>
373
+ New SSE Client connected: #<StringIO:0x007fd58647ae38>
374
+ SSE Client disconnected: #<StringIO:0x007fd58647ae38> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
375
+ Starting SSE heartbeat thread!
376
+ Starting SSE heartbeat thread!
377
+ New SSE Client connected: #<StringIO:0x007fcecbe52a30>
378
+ SSE Client disconnected: #<StringIO:0x007fcecbe52a30> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
379
+ Starting SSE heartbeat thread!
380
+ New SSE Client connected: #<StringIO:0x007fcecbdf28d8>
381
+ New SSE Client connected: #<StringIO:0x007fcecbdf3120>
382
+ SSE Client disconnected: #<StringIO:0x007fcecbdf3120> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
383
+ Starting SSE heartbeat thread!
384
+ Starting SSE heartbeat thread!
385
+ New SSE Client connected: #<StringIO:0x007fa16ca24c38>
386
+ SSE Client disconnected: #<StringIO:0x007fa16ca24c38> - uninitialized constant SseRailsEngine::Connection::HEARTBEAT_EVENT
387
+ Starting SSE heartbeat thread!
388
+ Starting SSE heartbeat thread!
389
+ New SSE Client connected: #<StringIO:0x007fca446a1588>
390
+ Starting SSE heartbeat thread!
391
+ Starting SSE heartbeat thread!
392
+ New SSE Client connected: #<StringIO:0x007ff605c1a8d8>
393
+ Starting SSE heartbeat thread!
394
+ Starting SSE heartbeat thread!
395
+ New SSE Client connected: #<StringIO:0x007ffc9df80218>
396
+ Starting SSE heartbeat thread!
397
+ New SSE Client connected: #<StringIO:0x007ffc9df53f60>
398
+ Starting SSE heartbeat thread!
399
+ New SSE Client connected: #<StringIO:0x007ffc9df41590>
400
+ New SSE Client connected: #<StringIO:0x007ffc9df41d10>
401
+ Starting SSE heartbeat thread!
402
+ Starting SSE heartbeat thread!
403
+ New SSE Client connected: #<StringIO:0x007ffc9deb9cd0>
404
+ Starting SSE heartbeat thread!
405
+ Starting SSE heartbeat thread!
406
+ New SSE Client connected: #<StringIO:0x007ffc9de88c48>
407
+ Starting SSE heartbeat thread!
408
+ New SSE Client connected: #<StringIO:0x007ffc9de5aa00>
409
+ Starting SSE heartbeat thread!
410
+ Starting SSE heartbeat thread!
411
+ New SSE Client connected: #<StringIO:0x007ffc9de19e38>
412
+ Starting SSE heartbeat thread!
413
+ New SSE Client connected: #<StringIO:0x007ffc9ddf84e0>
414
+ SSE Client disconnected: #<StringIO:0x007ffc9ddf84e0> - IOError
415
+ Starting SSE heartbeat thread!
416
+ Starting SSE heartbeat thread!
417
+ New SSE Client connected: #<StringIO:0x007f9fa485bb50>
418
+ New SSE Client connected: #<StringIO:0x007f9fa48682d8>
419
+ Starting SSE heartbeat thread!
420
+ Starting SSE heartbeat thread!
421
+ New SSE Client connected: #<StringIO:0x007fcfbb8983d8>
422
+ Starting SSE heartbeat thread!
423
+ New SSE Client connected: #<StringIO:0x007fcfbb843450>
424
+ Starting SSE heartbeat thread!
425
+ Starting SSE heartbeat thread!
426
+ New SSE Client connected: #<StringIO:0x007fcfbb8113d8>
427
+ New SSE Client connected: #<StringIO:0x007fcfbb8125d0>
428
+ Starting SSE heartbeat thread!
429
+ Starting SSE heartbeat thread!
430
+ New SSE Client connected: #<StringIO:0x007fcfbb7c8ac0>
431
+ SSE Client disconnected: #<StringIO:0x007fcfbb7c8ac0> - IOError
432
+ Starting SSE heartbeat thread!
433
+ New SSE Client connected: #<StringIO:0x007fcfbb7a8978>
434
+ Starting SSE heartbeat thread!
435
+ Starting SSE heartbeat thread!
436
+ New SSE Client connected: #<StringIO:0x007fcfbb759990>
437
+ Starting SSE heartbeat thread!
438
+ New SSE Client connected: #<StringIO:0x007fcfbb73b710>
439
+ Starting SSE heartbeat thread!
440
+ New SSE Client connected: #<StringIO:0x007fcfbb71a538>
441
+ Starting SSE heartbeat thread!
442
+ New SSE Client connected: #<StringIO:0x007fcfbb6f9158>
443
+ Starting SSE heartbeat thread!
444
+ Starting SSE heartbeat thread!
445
+ Starting SSE heartbeat thread!
446
+ New SSE Client connected: #<StringIO:0x007f807cdaa4e0>
447
+ Starting SSE heartbeat thread!
448
+ New SSE Client connected: #<StringIO:0x007f807cd88bd8>
449
+ SSE Client disconnected: #<StringIO:0x007f807cd88bd8> - IOError
450
+ Starting SSE heartbeat thread!
451
+ New SSE Client connected: #<StringIO:0x007f807cd69300>
452
+ Starting SSE heartbeat thread!
453
+ New SSE Client connected: #<StringIO:0x007f807cd4af18>
454
+ Starting SSE heartbeat thread!
455
+ New SSE Client connected: #<StringIO:0x007f807cd38d90>
456
+ Starting SSE heartbeat thread!
457
+ New SSE Client connected: #<StringIO:0x007f807cd19418>
458
+ New SSE Client connected: #<StringIO:0x007f807cd19fa8>
459
+ Starting SSE heartbeat thread!
460
+ New SSE Client connected: #<StringIO:0x007f807ccebbf8>
461
+ Starting SSE heartbeat thread!
462
+ Starting SSE heartbeat thread!
463
+ New SSE Client connected: #<StringIO:0x007f807ccaaa68>
464
+ Starting SSE heartbeat thread!
465
+ New SSE Client connected: #<StringIO:0x007f807cc8a560>
466
+ Starting SSE heartbeat thread!
467
+ Starting SSE heartbeat thread!
468
+ Starting SSE heartbeat thread!
469
+ New SSE Client connected: #<StringIO:0x007fee75441978>
470
+ Starting SSE heartbeat thread!
471
+ New SSE Client connected: #<StringIO:0x007fee753dbf88>
472
+ SSE Client disconnected: #<StringIO:0x007fee753dbf88> - IOError
473
+ Starting SSE heartbeat thread!
474
+ New SSE Client connected: #<StringIO:0x007fee753bac20>
475
+ Starting SSE heartbeat thread!
476
+ New SSE Client connected: #<StringIO:0x007fee7539ada8>
477
+ Starting SSE heartbeat thread!
478
+ New SSE Client connected: #<StringIO:0x007fee7537abe8>
479
+ New SSE Client connected: #<StringIO:0x007fee7537b430>
480
+ Starting SSE heartbeat thread!
481
+ New SSE Client connected: #<StringIO:0x007fee7535b2c0>
482
+ Starting SSE heartbeat thread!
483
+ New SSE Client connected: #<StringIO:0x007fee753496b0>
484
+ Starting SSE heartbeat thread!
485
+ New SSE Client connected: #<StringIO:0x007fee75328640>
486
+ SSE Client disconnected: #<StringIO:0x007fee75328640> - IOError
487
+ Starting SSE heartbeat thread!
488
+ Starting SSE heartbeat thread!
489
+ Starting SSE heartbeat thread!
490
+ New SSE Client connected: #<StringIO:0x007fee752ab578>
491
+ Starting SSE heartbeat thread!
492
+ Starting SSE heartbeat thread!
493
+ Starting SSE heartbeat thread!
494
+ New SSE Client connected: #<StringIO:0x007ff5753a8f00>
495
+ Starting SSE heartbeat thread!
496
+ New SSE Client connected: #<StringIO:0x007ff57538ae38>
497
+ Starting SSE heartbeat thread!
498
+ Starting SSE heartbeat thread!
499
+ New SSE Client connected: #<StringIO:0x007ff575370fd8>
500
+ New SSE Client connected: #<StringIO:0x007ff575371500>
501
+ Starting SSE heartbeat thread!
502
+ Starting SSE heartbeat thread!
503
+ Starting SSE heartbeat thread!
504
+ New SSE Client connected: #<StringIO:0x007ff575352740>
505
+ Starting SSE heartbeat thread!
506
+ New SSE Client connected: #<StringIO:0x007ff57534b760>
507
+ Starting SSE heartbeat thread!
508
+ New SSE Client connected: #<StringIO:0x007ff575343b50>
509
+ SSE Client disconnected: #<StringIO:0x007ff575343b50> - IOError
510
+ Starting SSE heartbeat thread!
511
+ New SSE Client connected: #<StringIO:0x007ff57533b568>
512
+ Starting SSE heartbeat thread!
513
+ New SSE Client connected: #<StringIO:0x007ff5753380e8>
514
+ Starting SSE heartbeat thread!
515
+ New SSE Client connected: #<StringIO:0x007ff57532aec0>
516
+ SSE Client disconnected: #<StringIO:0x007ff57532aec0> - IOError
517
+ Starting SSE heartbeat thread!
518
+ Starting SSE heartbeat thread!
519
+ New SSE Client connected: #<StringIO:0x007f864bdfb438>
520
+ SSE Client disconnected: #<StringIO:0x007f864bdfb438> - IOError
521
+ Starting SSE heartbeat thread!
522
+ New SSE Client connected: #<StringIO:0x007f864bdfa5d8>
523
+ Starting SSE heartbeat thread!
524
+ New SSE Client connected: #<StringIO:0x007f864bdeba60>
525
+ Starting SSE heartbeat thread!
526
+ Starting SSE heartbeat thread!
527
+ Starting SSE heartbeat thread!
528
+ New SSE Client connected: #<StringIO:0x007f864bdd94f0>
529
+ Starting SSE heartbeat thread!
530
+ New SSE Client connected: #<StringIO:0x007f864bdd12a0>
531
+ Starting SSE heartbeat thread!
532
+ New SSE Client connected: #<StringIO:0x007f864bdc9488>
533
+ SSE Client disconnected: #<StringIO:0x007f864bdc9488> - IOError
534
+ Starting SSE heartbeat thread!
535
+ Starting SSE heartbeat thread!
536
+ New SSE Client connected: #<StringIO:0x007f864bdc0680>
537
+ Starting SSE heartbeat thread!
538
+ New SSE Client connected: #<StringIO:0x007f864bdb9a88>
539
+ Starting SSE heartbeat thread!
540
+ New SSE Client connected: #<StringIO:0x007f864bdb27b0>
541
+ New SSE Client connected: #<StringIO:0x007f864bdb2f58>
542
+ Starting SSE heartbeat thread!
543
+ Starting SSE heartbeat thread!
544
+ New SSE Client connected: #<StringIO:0x007fdd7a921c10>
545
+ Starting SSE heartbeat thread!
546
+ New SSE Client connected: #<StringIO:0x007fdd7a91ae10>
547
+ Starting SSE heartbeat thread!
548
+ Starting SSE heartbeat thread!
549
+ New SSE Client connected: #<StringIO:0x007fdd7a90bfa0>
550
+ SSE Client disconnected: #<StringIO:0x007fdd7a90bfa0> - IOError
551
+ Starting SSE heartbeat thread!
552
+ New SSE Client connected: #<StringIO:0x007fdd7a9087b0>
553
+ New SSE Client connected: #<StringIO:0x007fdd7a908e68>
554
+ Starting SSE heartbeat thread!
555
+ New SSE Client connected: #<StringIO:0x007fdd7a9019b0>
556
+ Starting SSE heartbeat thread!
557
+ New SSE Client connected: #<StringIO:0x007fdd7a8fa778>
558
+ Starting SSE heartbeat thread!
559
+ New SSE Client connected: #<StringIO:0x007fdd7a8f8270>
560
+ Starting SSE heartbeat thread!
561
+ Starting SSE heartbeat thread!
562
+ Starting SSE heartbeat thread!
563
+ New SSE Client connected: #<StringIO:0x007fdd7a8daf18>
564
+ SSE Client disconnected: #<StringIO:0x007fdd7a8daf18> - IOError
565
+ Starting SSE heartbeat thread!
566
+ New SSE Client connected: #<StringIO:0x007fdd7a8d27c8>
@@ -2,11 +2,8 @@ require_relative '../../test_helper'
2
2
 
3
3
  describe SseRailsEngine::Manager do
4
4
  let(:manager) { SseRailsEngine.manager }
5
- let(:env) do
6
- Hashie::Mash.new('rack.hijack?' => true,
7
- 'rack.hijack' => ->() {},
8
- 'rack.hijack_io' => StringIO.new)
9
- end
5
+ let(:env1) { Hashie::Mash.new('rack.hijack?' => true, 'rack.hijack' => ->() {}, 'rack.hijack_io' => StringIO.new) }
6
+ let(:env2) { Hashie::Mash.new('rack.hijack?' => true, 'rack.hijack' => ->() {}, 'rack.hijack_io' => StringIO.new) }
10
7
 
11
8
  before do
12
9
  SseRailsEngine.instance_variable_set(:@manager, nil)
@@ -14,13 +11,13 @@ describe SseRailsEngine::Manager do
14
11
  end
15
12
 
16
13
  it 'ensures rack supports hijacking' do
17
- env['rack.hijack'] = nil
18
- assert_raises(SseRailsEngine::Manager::RackHijackUnsupported) { manager.register(env) }
14
+ env1['rack.hijack'] = nil
15
+ assert_raises(SseRailsEngine::Manager::RackHijackUnsupported) { manager.register(env1) }
19
16
  end
20
17
 
21
18
  it 'registers new response streams' do
22
19
  manager.connections.size.must_equal 0
23
- manager.register(env)
20
+ manager.register(env1)
24
21
  manager.connections.size.must_equal 1
25
22
  end
26
23
 
@@ -31,36 +28,38 @@ describe SseRailsEngine::Manager do
31
28
  it 'closes connection when client disconnects' do
32
29
  ActionController::Live::SSE.any_instance.stubs(:write).raises(IOError)
33
30
 
34
- manager.register(env)
31
+ manager.register(env1)
35
32
  manager.connections.size.must_equal 1
36
33
  manager.send_event('foo', 'bar')
37
34
  manager.connections.size.must_equal 0
38
35
  end
39
36
 
40
37
  it 'closes connection when failed sending event to client' do
41
- ActionController::Live::SSE.any_instance.stubs(:write).raises(RuntimeError)
38
+ ActionController::Live::SSE.any_instance.stubs(:write).raises(IOError)
42
39
 
43
- manager.register(env)
40
+ manager.register(env1)
44
41
  manager.connections.size.must_equal 1
45
42
  manager.send_event('foo', 'bar')
46
43
  manager.connections.size.must_equal 0
47
44
  end
48
45
 
49
46
  it 'writes string event to stream' do
50
- manager.register(env)
47
+ manager.register(env1)
51
48
  manager.send_event('foo', 'bar')
52
- env['rack.hijack_io'].string.must_equal(SseRailsEngine::Manager::SSE_HEADER + "event: foo\ndata: bar\n\n")
49
+ env1['rack.hijack_io'].string.must_equal(SseRailsEngine::Connection::SSE_HEADER + "event: foo\ndata: bar\n\n")
53
50
  end
54
51
 
55
52
  it 'writes event object to stream' do
56
- manager.register(env)
53
+ manager.register(env1)
57
54
  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")
55
+ env1['rack.hijack_io'].string.must_equal(
56
+ SseRailsEngine::Connection::SSE_HEADER +
57
+ "event: foo\ndata: {\"a\":123,\"b\":\"abc\",\"c\":{\"foo\":\"bar\"}}\n\n"
58
+ )
60
59
  end
61
60
 
62
61
  it 'writes minimum headers to rack middleware' do
63
- manager.call(env).must_equal [-1, {}, []]
62
+ manager.call(env1).must_equal [-1, {}, []]
64
63
  end
65
64
 
66
65
  it 'ensures heartbeat is sent' do
@@ -70,4 +69,31 @@ describe SseRailsEngine::Manager do
70
69
  SseRailsEngine::Manager.new
71
70
  sleep 0.2
72
71
  end
72
+
73
+ it 'sends events to connections that register for that channel' do
74
+ env1['QUERY_STRING'] = 'channels=foo,bar'
75
+ manager.register(env1)
76
+ manager.send_event('foo')
77
+ env1['rack.hijack_io'].string.must_equal(
78
+ SseRailsEngine::Connection::SSE_HEADER + "event: foo\ndata: \n\n", 'env1 should have received event')
79
+ end
80
+
81
+ it 'does not send events to clients that didnt register for them' do
82
+ env1['QUERY_STRING'] = 'channels=foo,bar'
83
+ manager.register(env1)
84
+ manager.send_event('test')
85
+ env1['rack.hijack_io'].string.must_equal(
86
+ SseRailsEngine::Connection::SSE_HEADER, 'env2 should not have received event')
87
+ end
88
+
89
+ it 'filters msgs depending on channels requested' do
90
+ env2['QUERY_STRING'] = 'channels=foo,bar'
91
+ manager.register(env1)
92
+ manager.register(env2)
93
+ manager.send_event('test')
94
+ env1['rack.hijack_io'].string.must_equal(
95
+ SseRailsEngine::Connection::SSE_HEADER + "event: test\ndata: \n\n", 'env1 should have received event')
96
+ env2['rack.hijack_io'].string.must_equal(
97
+ SseRailsEngine::Connection::SSE_HEADER, 'env2 should not have received event')
98
+ end
73
99
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sse-rails-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Hender
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
11
+ date: 2015-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -84,16 +84,16 @@ dependencies:
84
84
  name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '0.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '0.0'
97
97
  description: See the README.md at https://github.com/henders/sse-rails-engine
98
98
  email:
99
99
  - henders@gmail.com
@@ -107,6 +107,7 @@ files:
107
107
  - config/initializers/sse_rails_engine.rb
108
108
  - config/routes.rb
109
109
  - lib/sse-rails-engine.rb
110
+ - lib/sse_rails_engine/connection.rb
110
111
  - lib/sse_rails_engine/engine.rb
111
112
  - lib/sse_rails_engine/manager.rb
112
113
  - lib/sse_rails_engine/version.rb