Skip to content

Auto-reconnect PackagerConnection to Metro dev server (#56625)#56625

Closed
javache wants to merge 1 commit into
react:mainfrom
javache:export-D97823787
Closed

Auto-reconnect PackagerConnection to Metro dev server (#56625)#56625
javache wants to merge 1 commit into
react:mainfrom
javache:export-D97823787

Conversation

@javache

@javache javache commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Summary:

When MWA starts before Metro is running, PackagerConnection's WebSocket connection to Metro's /message endpoint silently fails with no retry. The developer must restart MWA after starting Metro to get HMR working.

This diff adds reconnection logic to PackagerConnection so it automatically retries when Metro becomes available. On initial connect failure or WebSocket disconnect, PackagerConnection schedules a retry after 5 seconds using the existing WebSocketClientFactory to create a fresh client. On successful reconnect, it fires liveReloadCallback_() which triggers FoxReactHost::reloadReactInstance() to load the bundle from Metro and set up HMR.

The reconnection is event-driven: each retry is a short-lived thread that sleeps 5 seconds then calls attemptConnection(). The connect callback handles success/failure — no long-lived polling thread. The active_ flag is set to false in the destructor to stop retries during shutdown.

Combined with D97570592's push-based route invalidation, this completes the dev loop: Metro can start at any time, PackagerConnection auto-reconnects, HMR keeps the bundle fresh, and route changes propagate automatically.

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D97823787

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 27, 2026
@meta-codesync

meta-codesync Bot commented Apr 27, 2026

Copy link
Copy Markdown

@javache has exported this pull request. If you are a Meta employee, you can view the originating Diff in D97823787.

@meta-codesync meta-codesync Bot changed the title Auto-reconnect PackagerConnection to Metro dev server Auto-reconnect PackagerConnection to Metro dev server (#56625) May 6, 2026
javache added a commit to javache/react-native that referenced this pull request May 6, 2026
Summary:

When MWA starts before Metro is running, PackagerConnection's WebSocket connection to Metro's `/message` endpoint silently fails with no retry. The developer must restart MWA after starting Metro to get HMR working.

This diff adds reconnection logic to PackagerConnection so it automatically retries when Metro becomes available. On initial connect failure or WebSocket disconnect, PackagerConnection schedules a retry after 5 seconds using the existing `WebSocketClientFactory` to create a fresh client. On successful reconnect, it fires `liveReloadCallback_()` which triggers `FoxReactHost::reloadReactInstance()` to load the bundle from Metro and set up HMR.

The reconnection is event-driven: each retry is a short-lived thread that sleeps 5 seconds then calls `attemptConnection()`. The connect callback handles success/failure — no long-lived polling thread. The `active_` flag is set to false in the destructor to stop retries during shutdown.

Combined with D97570592's push-based route invalidation, this completes the dev loop: Metro can start at any time, PackagerConnection auto-reconnects, HMR keeps the bundle fresh, and route changes propagate automatically.

Reviewed By: christophpurrer

Differential Revision: D97823787
@javache javache force-pushed the export-D97823787 branch from 47a1ff5 to 362f070 Compare May 6, 2026 10:39
Summary:

When MWA starts before Metro is running, PackagerConnection's WebSocket connection to Metro's `/message` endpoint silently fails with no retry. The developer must restart MWA after starting Metro to get HMR working.

This diff adds reconnection logic to PackagerConnection so it automatically retries when Metro becomes available. On initial connect failure or WebSocket disconnect, PackagerConnection schedules a retry after 5 seconds using the existing `WebSocketClientFactory` to create a fresh client. On successful reconnect, it fires `liveReloadCallback_()` which triggers `FoxReactHost::reloadReactInstance()` to load the bundle from Metro and set up HMR.

The reconnection is event-driven: each retry is a short-lived thread that sleeps 5 seconds then calls `attemptConnection()`. The connect callback handles success/failure — no long-lived polling thread. The `active_` flag is set to false in the destructor to stop retries during shutdown.

Combined with D97570592's push-based route invalidation, this completes the dev loop: Metro can start at any time, PackagerConnection auto-reconnects, HMR keeps the bundle fresh, and route changes propagate automatically.

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D97823787
@javache javache force-pushed the export-D97823787 branch from 362f070 to 41b719b Compare May 6, 2026 11:20
@meta-codesync meta-codesync Bot closed this in 882be91 May 6, 2026
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @javache in 882be91

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label May 6, 2026
@meta-codesync

meta-codesync Bot commented May 6, 2026

Copy link
Copy Markdown

This pull request has been merged in 882be91.

@javache javache deleted the export-D97823787 branch May 6, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants