Class: VSM::Lens::SSEBody
- Inherits:
-
Object
- Object
- VSM::Lens::SSEBody
- Defined in:
- lib/vsm/lens/server.rb
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(hub, queue, snapshot) ⇒ SSEBody
constructor
A new instance of SSEBody.
Constructor Details
#initialize(hub, queue, snapshot) ⇒ SSEBody
Returns a new instance of SSEBody.
161 162 163 164 |
# File 'lib/vsm/lens/server.rb', line 161 def initialize(hub, queue, snapshot) @hub, @queue, @snapshot = hub, queue, snapshot @heartbeat = true end |
Instance Method Details
#each ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/vsm/lens/server.rb', line 166 def each # Send snapshot first @snapshot.each { |ev| yield "data: #{JSON.generate(ev)}\n\n" } # Heartbeat thread to keep connections alive hb = Thread.new do while @heartbeat sleep 15 yield ": ping\n\n" # SSE comment line end end # Stream live events loop do ev = @queue.pop yield "data: #{JSON.generate(ev)}\n\n" end ensure @heartbeat = false @hub.unsubscribe(@queue) rescue nil hb.kill if hb&.alive? end |