Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/xrpl/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr

## Unreleased

### Fixed
* Fix event listener accumulation bug where `'connected'` event handlers would fire multiple times after each reconnection. The fix cleans up stale listeners from previous reconnect attempts to prevent duplicate event emissions on flaky connections with multiple sequential reconnect attempts.

## 4.6.0 (2026-02-12)

### Added
Expand Down
13 changes: 12 additions & 1 deletion packages/xrpl/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,19 @@ class Client extends EventEmitter<EventTypes> {
this.emit('error', errorCode, errorMessage, data)
})

let connectedListener: (() => void) | undefined
this.connection.on('reconnect', () => {
this.connection.on('connected', () => this.emit('connected'))
// Clean up any stale listener from previous reconnect attempt
// This prevents duplicate event emissions when multiple reconnect attempts
// occur before a successful connection (e.g., on flaky networks)
if (connectedListener !== undefined) {
this.connection.off('connected', connectedListener)
}

connectedListener = (): boolean => this.emit('connected')
// Use .once() so the listener auto-removes after firing
// This prevents listener accumulation across reconnections
this.connection.once('connected', connectedListener)
})

this.connection.on('disconnected', (code: number) => {
Expand Down
Loading