Skip to content

Log broadcasts once per event instead of once per subscriber (#73)#107

Open
russ wants to merge 1 commit into
cable-cr:masterfrom
russ:fix-73-log-broadcast-once
Open

Log broadcasts once per event instead of once per subscriber (#73)#107
russ wants to merge 1 commit into
cable-cr:masterfrom
russ:fix-73-log-broadcast-once

Conversation

@russ

@russ russ commented May 29, 2026

Copy link
Copy Markdown
Contributor

Problem

Fixes #73.

Cable::Server#send_to_channels logged the "transmitting" line inside the per-subscriber loop:

@channels[channel_identifier].each do |channel|
  ...
  Cable::Logger.info { "#{channel.class} transmitting #{parsed_message} (via streamed from #{channel.stream_identifier})" }
  channel.connection.socket.send(...)
end

So broadcasting one message to a stream with N connected clients produced N identical log lines. A chat room with 100 viewers logged "hi" 100 times; the only workaround was Cable::Logger.level = :none, which silences all Cable logging.

Change

Move the log outside the loop and emit a single line per broadcast event, including the number of subscribers it was delivered to:

Transmitting {"message" => "hi"} to 13 subscribers (via streamed from chat_room_1)
  • Counts only live connections actually transmitted to (closed connections are still reaped in the loop as before).
  • Skips the log entirely when there are no live subscribers.

The redundant per-message broadcast is still logged once at the call site (Channel#broadcast / .broadcast_to), so no information is lost.

Tests

Added spec/cable/server_spec.cr#send_to_channels — subscribes two connections to the same stream, captures logs, and asserts exactly one info line (... to 2 subscribers ...) is emitted. Full suite green (52 examples, 0 failures); ameba clean.

Note

This changes the log line format (<Class> transmitting <msg> (via streamed from <id>)Transmitting <msg> to <n> subscribers (via streamed from <id>)). Anything grepping the old string will need updating.

🤖 Generated with Claude Code

…able-cr#73

`Cable::Server#send_to_channels` logged a "transmitting" line inside the
per-subscriber loop, so a single broadcast to a stream with N connected
clients produced N identical log lines. A busy chat room could emit
dozens of duplicate entries for one message, flooding production logs.

Move the log outside the loop and emit a single line per broadcast that
includes the number of subscribers it was delivered to. Skips logging
entirely when there are no live subscribers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@russ russ marked this pull request as ready for review May 29, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logging issues

1 participant