Module: Message::Broadcasting

Extended by:
ActiveSupport::Concern
Included in:
Message
Defined in:
app/models/concerns/message/broadcasting.rb

Overview

Broadcasts Message records to connected WebSocket clients via ActionCable. Follows the Turbo Streams pattern: messages are broadcast on both create and update, with an action type so clients can distinguish append from replace operations.

Each broadcast includes the Message’s database ID, enabling clients to maintain an ID-indexed store for efficient in-place updates (e.g. when token counts arrive asynchronously from CountMessageTokensJob).

When a new message pushes old messages out of the LLM’s context window, the broadcast includes ‘evicted_message_ids` so clients can remove phantom messages that the agent no longer knows about.

Examples:

Create broadcast payload

{
  "type" => "user_message", "content" => "hello", ...,
  "id" => 42, "action" => "create",
  "rendered" => { "basic" => { "role" => "user", "content" => "hello" } }
}

Broadcast with viewport evictions

{
  "type" => "agent_message", "content" => "...", ...,
  "id" => 99, "action" => "create",
  "evicted_message_ids" => [101, 102, 103]
}

Update broadcast payload (e.g. token count arrives)

{
  "type" => "user_message", "content" => "hello", ...,
  "id" => 42, "action" => "update",
  "rendered" => { "debug" => { "role" => "user", "content" => "hello", "tokens" => 15 } }
}

Constant Summary collapse

ACTION_CREATE =
"create"
ACTION_UPDATE =
"update"