Skip to content

Auto-reconnect PackagerConnection to Metro dev server#56625

Open
javache wants to merge 1 commit intofacebook:mainfrom
javache:export-D97823787
Open

Auto-reconnect PackagerConnection to Metro dev server#56625
javache wants to merge 1 commit intofacebook:mainfrom
javache:export-D97823787

Conversation

@javache
Copy link
Copy Markdown
Member

@javache javache commented Apr 27, 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.

Differential Revision: D97823787

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.

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
Copy link
Copy Markdown

meta-codesync Bot commented Apr 27, 2026

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

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 meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant