Module: R4S

Defined in:
lib/r4s.rb

Defined Under Namespace

Classes: SSE

Constant Summary collapse

SSES =
ThreadSafe::Hash.new

Class Method Summary collapse

Class Method Details

.add_stream(response, session, key = "none") ⇒ Object

The session_id is used to identify users who already have a connection The key is used to group “identical” sse-streams so you can push data to a group of streams



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/r4s.rb', line 8

def self.add_stream(response,session,key="none")

  id = session["session_id"]

  R4S::SSES[key] = ThreadSafe::Hash.new unless R4S::SSES.keys.include?(key)

  if R4S::SSES[key].keys.include?(id)
    sse = R4S::SSES[key][id]
    sse.close
  end

  sse = R4S::SSE.new(response,key,id)
  R4S::SSES[key][id] = sse

  return sse
end

.push_data(key, data, options = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/r4s.rb', line 25

def self.push_data(key,data,options={})
  if R4S::SSES.keys.include?(key)
    R4S::SSES[key].each do |k,sse|
      sse.write(data,options)
    end
  end
end