Class: PromptObjects::Server::WebSocketHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/server/websocket_handler.rb

Overview

Handles WebSocket connections for real-time communication with the frontend. Subscribes to MessageBus for state updates and handles client messages.

Runtime snapshots establish authoritative state; versioned events then incrementally update runs, messages, artifacts, and environment data.

Instance Method Summary collapse

Constructor Details

#initialize(runtime:, connection:, app: nil) ⇒ WebSocketHandler

Returns a new instance of WebSocketHandler.



14
15
16
17
18
19
20
21
# File 'lib/prompt_objects/server/websocket_handler.rb', line 14

def initialize(runtime:, connection:, app: nil)
  @runtime = runtime
  @connection = connection
  @app = app
  @write_mutex = Mutex.new
  @subscribed = false
  @bus_subscription = nil
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
# File 'lib/prompt_objects/server/websocket_handler.rb', line 23

def run
  subscribe_to_bus
  send_initial_state
  read_loop
ensure
  unsubscribe_from_bus
end

#send_message(data) ⇒ Object

Send a message to this client (public for broadcasting).



32
33
34
35
36
37
38
39
40
# File 'lib/prompt_objects/server/websocket_handler.rb', line 32

def send_message(data)
  json = JSON.generate(data)
  @write_mutex.synchronize do
    @connection.write(json)
    @connection.flush
  end
rescue => e
  puts "WebSocket write error: #{e.message}" if ENV["DEBUG"]
end