From a5f2646520af0070a8fc18537b403a0cd51f130a Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Sat, 24 Oct 2026 12:57:15 +0200 Subject: [PATCH 1/3] Catch up from where the connection dropped When a room page reconnects, it asks for the messages created since the newest one it has. A message that came into the page while it was disconnected moved that point too: one sent from the page, which its answer appends, or a live one that arrived before the page counted itself connected again. Messages others posted before it, while the connection was down, were then never asked for, and the page went on without them until a reload. Only messages that come in while connected now move where the next refresh starts. Co-Authored-By: Claude Opus 5.5 --- .../controllers/refresh_room_controller.js | 10 ++- ...hing_up_after_a_dropped_connection_test.rb | 76 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 test/system/catching_up_after_a_dropped_connection_test.rb diff --git a/app/javascript/controllers/refresh_room_controller.js b/app/javascript/controllers/refresh_room_controller.js index 3c177b1e6..84123392f 100644 --- a/app/javascript/controllers/refresh_room_controller.js +++ b/app/javascript/controllers/refresh_room_controller.js @@ -11,6 +11,7 @@ export default class extends Controller { static values = { loadedAt: Number, url: String, } #lastLoadedAt + #connected = false #offlineTimer = null #hiddenAt = null @@ -30,8 +31,12 @@ export default class extends Controller { this.channel?.unsubscribe() } + // Only a message that arrives while connected moves where the next refresh starts. One that arrives while + // disconnected, like the answer to a message sent then, can be newer than others missed meanwhile. messageTargetConnected(target) { - this.#lastLoadedAt = Math.max(this.#lastLoadedAt, target.dataset.messageUpdatedAt || 0) + if (this.#connected) { + this.#lastLoadedAt = Math.max(this.#lastLoadedAt, target.dataset.messageUpdatedAt || 0) + } } visibilityChanged() { @@ -53,6 +58,7 @@ export default class extends Controller { } #channelConnected() { + this.#connected = true this.#refresh("connection") clearTimeout(this.#offlineTimer) @@ -63,6 +69,8 @@ export default class extends Controller { this.#offlineTimer = setTimeout(() => { this.dispatch("offline", { target: window }) }, OFFLINE_AFTER_DISCONNECTED_TIMEOUT) + + this.#connected = false } #refresh(reason) { diff --git a/test/system/catching_up_after_a_dropped_connection_test.rb b/test/system/catching_up_after_a_dropped_connection_test.rb new file mode 100644 index 000000000..e6b109f0d --- /dev/null +++ b/test/system/catching_up_after_a_dropped_connection_test.rb @@ -0,0 +1,76 @@ +require "application_system_test_case" + +class CatchingUpAfterADroppedConnectionTest < ApplicationSystemTestCase + setup do + sign_in "jz@37signals.com" + join_room rooms(:designers) + + using_session("Kevin") do + sign_in "kevin@37signals.com" + join_room rooms(:designers) + end + end + + test "a message sent while the connection is down doesn't hide the ones missed before it" do + drop_connection + using_session("Kevin") { send_message "Missed while away" } + assert_no_message_text "Missed while away" + + send_message "Sent while away" + restore_connection + + assert_message_text "Missed while away", wait: 10 + assert_equal [ "Missed while away", "Sent while away" ], texts_from_while_away + end + + test "messages missed while the connection is down show once it's back, and the ones shown before stay as they are" do + using_session("Kevin") do + send_message "Seen live" + # The refresh asks from the newest message's millisecond, so that one comes again whatever happens. + send_message "Seen live, the newest" + end + mark_message "Seen live" + + drop_connection + using_session("Kevin") { send_message "Missed while away" } + assert_no_message_text "Missed while away" + + restore_connection + + assert_message_text "Missed while away", wait: 10 + assert_equal [ "Missed while away" ], texts_from_while_away + assert_selector ".message[data-marked]", text: "Seen live", count: 1 + end + + private + # The server drops the connection, as it does when the user signs out elsewhere, and Chrome keeps it from coming + # back until restore_connection, while the rest of the page still reaches the server. + def drop_connection + page.driver.browser.execute_cdp "Network.enable" + page.driver.browser.execute_cdp "Network.setBlockedURLs", urls: [ "*/cable*" ] + users(:jz).reset_remote_connections + assert_no_selector "turbo-cable-stream-source[connected]", visible: false + end + + def restore_connection + page.driver.browser.execute_cdp "Network.setBlockedURLs", urls: [] + # Action Cable tries again on its own schedule, several seconds apart. + assert_selector "turbo-cable-stream-source[connected]", count: 3, visible: false, wait: 30 + end + + # A refresh that brings the message again replaces its element, and the mark with it. + def mark_message(text) + assert_message_text text + execute_script <<~JS, text + Array.from(document.querySelectorAll(".message")).find((message) => message.textContent.includes(arguments[0])).dataset.marked = "true" + JS + end + + def texts_from_while_away + all(".message .lexxy-content", text: "while away").map(&:text) + end + + def assert_no_message_text(text) + assert_no_selector ".message__body", text: text + end +end From c46d58932b1e588eedaf2340cb33a153541f5fce Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Sat, 24 Oct 2026 13:51:08 +0200 Subject: [PATCH 2/3] Keep messages sent while disconnected before one that arrives live The refresh after a dropped connection now brings again the messages that came into the page meanwhile, like the ones sent from it. Turbo moves those to the end together with the missed ones, so a message that arrived live as the connection came back stayed above them. Put each of them back in its place, as was already done for the last one. Co-Authored-By: Claude Opus 5.5 --- .../controllers/messages_controller.js | 21 +++++++++++++++++++ ...hing_up_after_a_dropped_connection_test.rb | 19 +++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/javascript/controllers/messages_controller.js b/app/javascript/controllers/messages_controller.js index 905e42686..996968dc7 100644 --- a/app/javascript/controllers/messages_controller.js +++ b/app/javascript/controllers/messages_controller.js @@ -65,11 +65,14 @@ export default class extends Controller { if (upToDate) { event.detail.render = async (streamElement) => { + const shownMessageIds = this.#shownMessageIds(streamElement) + const didScroll = await this.#scrollManager.autoscroll(false, async () => { await render(streamElement) await nextEventLoopTick() this.#positionLastMessage() + this.#positionMessages(shownMessageIds) this.#playSoundForLastMessage() this.#paginator.trimExcessMessages(true) }) @@ -183,6 +186,24 @@ export default class extends Controller { } } + // Turbo moves a message the page already shows to the end, with the others the stream brings. The refresh after a + // dropped connection brings again the messages that came in meanwhile, and one that arrived live since would stay + // above them. + #shownMessageIds(streamElement) { + return Array.from(streamElement.templateContent.querySelectorAll(".message"), (message) => message.id) + .filter((id) => id && document.getElementById(id)?.parentElement === this.messagesTarget) + } + + #positionMessages(ids) { + for (const id of ids) { + const message = document.getElementById(id) + + if (message?.parentElement === this.messagesTarget) { + this.#followingMessage(message)?.before(message) + } + } + } + #playSoundForLastMessage() { const soundTarget = this.#lastMessage.querySelector(".sound") diff --git a/test/system/catching_up_after_a_dropped_connection_test.rb b/test/system/catching_up_after_a_dropped_connection_test.rb index e6b109f0d..7a6d29d0d 100644 --- a/test/system/catching_up_after_a_dropped_connection_test.rb +++ b/test/system/catching_up_after_a_dropped_connection_test.rb @@ -23,6 +23,24 @@ class CatchingUpAfterADroppedConnectionTest < ApplicationSystemTestCase assert_equal [ "Missed while away", "Sent while away" ], texts_from_while_away end + test "messages sent while the connection is down stay before one that arrives live as it comes back" do + drop_connection + send_message "Sent while away" + send_message "Sent again while away" + mark_message "Sent again while away" + + # As if it arrived live once the connection was back, between the refresh's query and its answer. + execute_script <<~JS, dom_id(rooms(:designers), :messages), 1.minute.from_now.to_fs(:epoch) + document.getElementById(arguments[0]).insertAdjacentHTML("beforeend", + `
Live while away
`) + JS + restore_connection + + # The refresh brings the newest one again whatever happens, so its mark goes once the refresh is in. + assert_no_selector ".message[data-marked]", wait: 10 + assert_equal [ "Sent while away", "Sent again while away", "Live while away" ], texts_from_while_away + end + test "messages missed while the connection is down show once it's back, and the ones shown before stay as they are" do using_session("Kevin") do send_message "Seen live" @@ -64,6 +82,7 @@ def mark_message(text) execute_script <<~JS, text Array.from(document.querySelectorAll(".message")).find((message) => message.textContent.includes(arguments[0])).dataset.marked = "true" JS + assert_selector ".message[data-marked]", text: text end def texts_from_while_away From 0514347d7b9860282ecdf3d375d9f260d697a43d Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Sat, 24 Oct 2026 14:52:01 +0200 Subject: [PATCH 3/3] Put every message a refresh brings in its place Putting back only the messages the page already showed left a missed message, and the ones after it, below a message that arrived live meanwhile. And which ones the page showed was read before the render waited its turn in the ScrollManager queue, so an earlier render still queued could change it. Place every message the stream brings, from the ids it carries when its turn comes. Only while the list is at its end: one that's catching up pages on from its last message. Co-Authored-By: Claude Opus 5.5 --- .../controllers/messages_controller.js | 17 ++++++++--------- ...tching_up_after_a_dropped_connection_test.rb | 5 +++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/javascript/controllers/messages_controller.js b/app/javascript/controllers/messages_controller.js index 996968dc7..643e7e205 100644 --- a/app/javascript/controllers/messages_controller.js +++ b/app/javascript/controllers/messages_controller.js @@ -65,14 +65,14 @@ export default class extends Controller { if (upToDate) { event.detail.render = async (streamElement) => { - const shownMessageIds = this.#shownMessageIds(streamElement) - const didScroll = await this.#scrollManager.autoscroll(false, async () => { + const messageIds = this.#messageIdsIn(streamElement) + await render(streamElement) await nextEventLoopTick() this.#positionLastMessage() - this.#positionMessages(shownMessageIds) + if (this.#paginator.upToDate) this.#positionMessages(messageIds) this.#playSoundForLastMessage() this.#paginator.trimExcessMessages(true) }) @@ -186,14 +186,13 @@ export default class extends Controller { } } - // Turbo moves a message the page already shows to the end, with the others the stream brings. The refresh after a - // dropped connection brings again the messages that came in meanwhile, and one that arrived live since would stay - // above them. - #shownMessageIds(streamElement) { - return Array.from(streamElement.templateContent.querySelectorAll(".message"), (message) => message.id) - .filter((id) => id && document.getElementById(id)?.parentElement === this.messagesTarget) + #messageIdsIn(streamElement) { + return Array.from(streamElement.templateContent.querySelectorAll(".message"), (message) => message.id).filter(Boolean) } + // The refresh after a dropped connection brings what was missed and, again, what came in meanwhile, which Turbo moves + // to the end with the rest: a message that arrived live since would stay above them all. Only while the list is at + // its end, as one that's catching up pages on from its last message. #positionMessages(ids) { for (const id of ids) { const message = document.getElementById(id) diff --git a/test/system/catching_up_after_a_dropped_connection_test.rb b/test/system/catching_up_after_a_dropped_connection_test.rb index 7a6d29d0d..0c8320771 100644 --- a/test/system/catching_up_after_a_dropped_connection_test.rb +++ b/test/system/catching_up_after_a_dropped_connection_test.rb @@ -23,8 +23,9 @@ class CatchingUpAfterADroppedConnectionTest < ApplicationSystemTestCase assert_equal [ "Missed while away", "Sent while away" ], texts_from_while_away end - test "messages sent while the connection is down stay before one that arrives live as it comes back" do + test "messages missed or sent while the connection is down stay before one that arrives live as it comes back" do drop_connection + using_session("Kevin") { send_message "Missed while away" } send_message "Sent while away" send_message "Sent again while away" mark_message "Sent again while away" @@ -38,7 +39,7 @@ class CatchingUpAfterADroppedConnectionTest < ApplicationSystemTestCase # The refresh brings the newest one again whatever happens, so its mark goes once the refresh is in. assert_no_selector ".message[data-marked]", wait: 10 - assert_equal [ "Sent while away", "Sent again while away", "Live while away" ], texts_from_while_away + assert_equal [ "Missed while away", "Sent while away", "Sent again while away", "Live while away" ], texts_from_while_away end test "messages missed while the connection is down show once it's back, and the ones shown before stay as they are" do