Skip to content

Commit

Permalink
fix: Re-dial peers when resuming from background on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Leblow committed Apr 12, 2024
1 parent ef0e2c6 commit 4deaca5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
// This method is only used on iOS through rn-bridge for reacting on lifecycle changes
public async openSocket() {
await this.socketService.init()
await this.libp2pService.redialPeers()
}

public async leaveCommunity() {
Expand Down
22 changes: 22 additions & 0 deletions packages/backend/src/nest/libp2p/libp2p.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ export class Libp2pService extends EventEmitter {
await this.libp2pInstance?.dial(multiaddr(peerAddress))
}

private hangUpPeer = async (peerAddress: string) => {
await this.libp2pInstance?.hangUp(multiaddr(peerAddress))
this.dialedPeers.delete(peerAddress)
}

/**
* Hang up existing peer connections and re-dial them. Specifically useful on
* iOS where Tor receives a new port when the app resumes from background and
* we want to close/re-open connections.
*/
public redialPeers = async () => {
this.logger('Re-dialing peers')

const dialedPeers = this.dialedPeers

for (const peerAddress of dialedPeers) {
await this.hangUpPeer(peerAddress)
}

this.processInChunksService.updateData(Array.from(dialedPeers))
}

public readonly createLibp2pAddress = (address: string, peerId: string): string => {
return createLibp2pAddress(address, peerId)
}
Expand Down

0 comments on commit 4deaca5

Please sign in to comment.