diff --git a/packages/core/src/relay-client.ts b/packages/core/src/relay-client.ts index 3f6fb29..91967a7 100644 --- a/packages/core/src/relay-client.ts +++ b/packages/core/src/relay-client.ts @@ -246,26 +246,40 @@ export class RelayClient extends EventEmitter { this.pairedPubkeyHex, ); - try { - await Promise.any( - this.pool.publish(this.config.explicitRelayUrls, wrapped), - ); - if (this.config.logNetworkActivity) { - debug(Scope.Relay, `Published message ${message.action} to relay`); - } - } catch (error) { - if (this.config.logNetworkActivity) { + const results = await Promise.allSettled( + this.pool.publish(this.config.explicitRelayUrls, wrapped), + ); + + const fulfilled = results.filter((r) => r.status === "fulfilled"); + const rejected = results.filter((r) => r.status === "rejected"); + + if (rejected.length > 0 && this.config.logNetworkActivity) { + for (const r of rejected) { logError( Scope.Relay, - `Failed to publish message ${message.action}:`, - error, + `Failed to publish ${message.action} to a relay:`, + (r as PromiseRejectedResult).reason, ); } - this.emitDisconnect( - error instanceof Error ? error : new Error(`Publish failed: ${error}`), + } + + if (fulfilled.length === 0) { + const error = new Error( + `Failed to publish ${message.action} to all relays`, ); + if (this.config.logNetworkActivity) { + logError(Scope.Relay, error.message); + } + this.emitDisconnect(error); throw error; } + + if (this.config.logNetworkActivity) { + debug( + Scope.Relay, + `Published message ${message.action} to ${fulfilled.length}/${results.length} relay(s)`, + ); + } } private handleWrappedEvent(wrappedEvent: NostrEvent): void {