Class: NanoGPT::Web::SSENotifier
- Inherits:
-
Object
- Object
- NanoGPT::Web::SSENotifier
- Defined in:
- lib/nano_gpt/web/sse_notifier.rb
Overview
Manages Server-Sent Events connections and broadcasts
Instance Method Summary collapse
- #add(connection) ⇒ Object
- #broadcast(type:, data:) ⇒ Object
-
#initialize ⇒ SSENotifier
constructor
A new instance of SSENotifier.
- #remove(connection) ⇒ Object
Constructor Details
#initialize ⇒ SSENotifier
Returns a new instance of SSENotifier.
9 10 11 12 |
# File 'lib/nano_gpt/web/sse_notifier.rb', line 9 def initialize @connections = [] @mutex = Mutex.new end |
Instance Method Details
#add(connection) ⇒ Object
14 15 16 |
# File 'lib/nano_gpt/web/sse_notifier.rb', line 14 def add(connection) @mutex.synchronize { @connections << connection } end |
#broadcast(type:, data:) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nano_gpt/web/sse_notifier.rb', line 22 def broadcast(type:, data:) payload = "event: #{type}\ndata: #{JSON.generate(data)}\n\n" @mutex.synchronize do @connections.reject! do |conn| begin conn << payload false rescue IOError, Errno::EPIPE true end end end end |
#remove(connection) ⇒ Object
18 19 20 |
# File 'lib/nano_gpt/web/sse_notifier.rb', line 18 def remove(connection) @mutex.synchronize { @connections.delete(connection) } end |