Skip to content

Conversation

@chakra-guy
Copy link
Collaborator

@chakra-guy chakra-guy commented Sep 25, 2025

This PR modifies the WebsocketTransport in a non-breaking way to use a shared Centrifugo client.

This is an optimisation that is useful for mobile (where we might end up having multiple WalletClient) by collapsing all the websocket connections into a single one under the hood.


Note

Introduce SharedCentrifuge and a useSharedConnection option to share a single websocket across instances, refine subscription/history handling, and add comprehensive integration tests.

  • Core:
    • Shared connection: Add SharedCentrifuge with reference-counted connections/subscriptions and proxy ISubscription API.
    • WebSocketTransport:
      • New option useSharedConnection to switch between Centrifuge and SharedCentrifuge.
      • subscribe reworked to avoid duplicate listeners, always fetch history on subscribe, and handle already-subscribed state.
      • Type updates to use ISubscription where needed; safe casts for removeSubscription.
  • Tests:
    • Add shared-centrifuge.integration.test.ts: connection sharing, ref counting, unsubscribe behavior, cleanup, and concurrency.
    • Expand index.integration.test.ts to run in both modes (shared/single), enforce publisher subscription before publish, and add checks for duplicate listeners and history fetch calls.
    • Adjust expectations for connection state and subscription presence (undefined/falsy vs null).

Written by Cursor Bugbot for commit b2f1ff2. This will update automatically on new commits. Configure here.

@chakra-guy chakra-guy marked this pull request as ready for review October 1, 2025 14:28
@chakra-guy chakra-guy changed the title Feat/shared ws connection Feat: Shared ws connection Oct 1, 2025
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@elribonazo
Copy link
Contributor

lgtm!

@chakra-guy chakra-guy merged commit 6afb38b into main Oct 2, 2025
12 checks passed
@chakra-guy chakra-guy deleted the feat/shared-ws-connection branch October 2, 2025 12:45
@chakra-guy chakra-guy mentioned this pull request Oct 7, 2025
github-merge-queue bot pushed a commit to MetaMask/metamask-mobile that referenced this pull request Oct 9, 2025
#20977)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

A [previous
PR](MetaMask/mobile-wallet-protocol#49) on
https://github.com/MetaMask/mobile-wallet-protocol/ modified the
`WebSocketTransport` in a non-breaking way to use a shared `Centrifugo`
client.

This PR brings in this change by updating the package and passing in the
necessary flag to allow usage of the shared `Centrifugo` client.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Changelog**

<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`

If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`

(This helps the Release Engineer do their job more quickly and
accurately)
-->

CHANGELOG entry: null

## **Related issues**

Fixes:
https://www.notion.so/metamask-consensys/SDK-Phase-1-End-game-276f86d67d688061a461ee0cbaf37649?p=285f86d67d6880b2b793f8bd4f584710&pm=s

## **Manual testing steps**

```gherkin
Feature: my feature name

  Scenario: user [verb for user action]
    Given [describe expected initial app state]

    When user [verb for user action]
    Then [describe expected outcome]
```

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**


https://github.com/user-attachments/assets/68649d66-c1de-4d9a-b1c3-82cae0029737

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Enables shared Centrifugo client by passing `useSharedConnection:
true` to `WebSocketTransport.create` and updates dependency to
`@metamask/mobile-wallet-protocol-core@^0.3.0`.
> 
> - **SDKConnectV2**:
> - **`app/core/SDKConnectV2/services/connection.ts`**: Pass
`useSharedConnection: true` when creating `WebSocketTransport`.
> - **Tests**: Update `connection.test.ts` to expect
`useSharedConnection: true` in `WebSocketTransport.create` args.
> - **Dependencies**:
> - Bump `@metamask/mobile-wallet-protocol-core` to `^0.3.0` (lockfile
updated).
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
86bdb9f. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
caieu pushed a commit to MetaMask/metamask-mobile that referenced this pull request Oct 9, 2025
#20977)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

A [previous
PR](MetaMask/mobile-wallet-protocol#49) on
https://github.com/MetaMask/mobile-wallet-protocol/ modified the
`WebSocketTransport` in a non-breaking way to use a shared `Centrifugo`
client.

This PR brings in this change by updating the package and passing in the
necessary flag to allow usage of the shared `Centrifugo` client.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Changelog**

<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`

If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`

(This helps the Release Engineer do their job more quickly and
accurately)
-->

CHANGELOG entry: null

## **Related issues**

Fixes:
https://www.notion.so/metamask-consensys/SDK-Phase-1-End-game-276f86d67d688061a461ee0cbaf37649?p=285f86d67d6880b2b793f8bd4f584710&pm=s

## **Manual testing steps**

```gherkin
Feature: my feature name

  Scenario: user [verb for user action]
    Given [describe expected initial app state]

    When user [verb for user action]
    Then [describe expected outcome]
```

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**


https://github.com/user-attachments/assets/68649d66-c1de-4d9a-b1c3-82cae0029737

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Enables shared Centrifugo client by passing `useSharedConnection:
true` to `WebSocketTransport.create` and updates dependency to
`@metamask/mobile-wallet-protocol-core@^0.3.0`.
> 
> - **SDKConnectV2**:
> - **`app/core/SDKConnectV2/services/connection.ts`**: Pass
`useSharedConnection: true` when creating `WebSocketTransport`.
> - **Tests**: Update `connection.test.ts` to expect
`useSharedConnection: true` in `WebSocketTransport.create` args.
> - **Dependencies**:
> - Bump `@metamask/mobile-wallet-protocol-core` to `^0.3.0` (lockfile
updated).
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
86bdb9f. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
weitingsun pushed a commit to MetaMask/metamask-mobile that referenced this pull request Oct 15, 2025
#20977)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

A [previous
PR](MetaMask/mobile-wallet-protocol#49) on
https://github.com/MetaMask/mobile-wallet-protocol/ modified the
`WebSocketTransport` in a non-breaking way to use a shared `Centrifugo`
client.

This PR brings in this change by updating the package and passing in the
necessary flag to allow usage of the shared `Centrifugo` client.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Changelog**

<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`

If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`

(This helps the Release Engineer do their job more quickly and
accurately)
-->

CHANGELOG entry: null

## **Related issues**

Fixes:
https://www.notion.so/metamask-consensys/SDK-Phase-1-End-game-276f86d67d688061a461ee0cbaf37649?p=285f86d67d6880b2b793f8bd4f584710&pm=s

## **Manual testing steps**

```gherkin
Feature: my feature name

  Scenario: user [verb for user action]
    Given [describe expected initial app state]

    When user [verb for user action]
    Then [describe expected outcome]
```

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**


https://github.com/user-attachments/assets/68649d66-c1de-4d9a-b1c3-82cae0029737

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Enables shared Centrifugo client by passing `useSharedConnection:
true` to `WebSocketTransport.create` and updates dependency to
`@metamask/mobile-wallet-protocol-core@^0.3.0`.
> 
> - **SDKConnectV2**:
> - **`app/core/SDKConnectV2/services/connection.ts`**: Pass
`useSharedConnection: true` when creating `WebSocketTransport`.
> - **Tests**: Update `connection.test.ts` to expect
`useSharedConnection: true` in `WebSocketTransport.create` args.
> - **Dependencies**:
> - Bump `@metamask/mobile-wallet-protocol-core` to `^0.3.0` (lockfile
updated).
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
86bdb9f. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants