fix: auto-reconnect relays after server-initiated disconnect

Previously, when a relay closed the WebSocket (or the connection
dropped), NostrClient.onDisconnected only updated state and notified
listeners — it never tried to reconnect. The relay then stayed
disconnected until the next subscribe/count/publish call (which
triggers reconnect()) or an explicit reconnect() from the caller.

Now, if the client is still active and the relay is still in the
desired set (i.e. some sub/count/outbox still wants it),
onDisconnected schedules a debounced reconnect via reconnectIfNeedsTo,
which respects per-relay exponential backoff so we don't hammer dead
relays.
This commit is contained in:
Claude
2026-05-04 20:17:29 +00:00
parent 8cea0fa869
commit 575fe952c6
@@ -295,6 +295,18 @@ class NostrClient(
override fun onDisconnected(relay: IRelayClient) {
activeRequests.onDisconnected(relay.url)
listeners.forEach { it.onDisconnected(relay) }
// If the client is still active and the relay is still in the
// desired set (i.e. there are subscriptions, counts or pending
// outbox events that want it), reconnect it. Without this, a
// server-initiated close leaves the relay disconnected until a
// subscription change or an explicit reconnect() arrives.
// The reconnect path is debounced and goes through
// reconnectIfNeedsTo, which respects each relay's exponential
// backoff so we don't hammer dead relays.
if (isActive && relay.url in allRelays.value) {
reconnect(onlyIfChanged = true)
}
}
override fun onCannotConnect(