Repository navigation
Send NetworkStatusChanged when the network status changes - #4
Open
wojt-janowski wants to merge 3 commits into
Open
wojt-janowski wants to merge 3 commits into
wojt-janowski wants to merge 3 commits into
Conversation
…ead of guessing offline on a slow first path
Network::status() is a snapshot, so apps polled it from screen hooks to notice going offline or coming back. The plugin now follows the network from launch and sends NativePHP\Network\Events\NetworkStatusChanged (BroadcastsGlobally) with the same four fields whenever they change. - iOS: the long-lived NWPathMonitor from NativePHP#3 starts in the plugin's init function and reports each path whose status differs from the last. - Android: registerDefaultNetworkCallback on the main looper, with the status computed from the capabilities each callback is given. - The launch status is the baseline and is not sent; repeats are dropped. - Requires nativephp/mobile ^4.0, where BroadcastsGlobally exists. Closes NativePHP#2 Amp-Thread-ID: https://ampcode.com/threads/T-01a1d176-5f7f-77ba-b41b-a985ab325986 Co-authored-by: Wojt Janowski <wojt@clinically.com.au>
wojt-janowski
marked this pull request as ready for review
November 12, 2026 21:34
Android reports the loss of the Wi-Fi network about 20 ms before it makes mobile data the default, so turning Wi-Fi off sent connected=false and then connected=true. Seen on an API 37 emulator. Hold a loss for one second and drop it if another network becomes the default first; a real disconnect is still reported, a second later. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d176-5f7f-77ba-b41b-a985ab325986 Co-authored-by: Wojt Janowski <wojt@clinically.com.au>
This was referenced Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #2. Builds on #3: this branch contains #3's commit, so merging #3 first leaves only the second commit here.
Network::status()is a snapshot, so apps notice going offline or coming back only by polling it, usually from a screen'sonResume(). That hook runs only when a pop reveals a screen, so the app misses the user coming back to it. This adds the event #2 proposes, in the shape it proposes.The change
NativePHP\Network\Events\NetworkStatusChanged(bool $connected, string $type, bool $isExpensive, bool $isConstrained),BroadcastsGlobally, listed innativephp.jsonevents. The properties are the fourstatus()fields.NWPathMonitornow starts in the plugin's init function (networkInit) and reports each path whose status differs from the last one. The status fields moved into a smallNetworkStatusstruct, soNetwork.Statusand the event build the same payload.registerDefaultNetworkCallbackon the main looper, started fromnetworkInit(context), which is safe to call more than once. The status is computed from the capabilities each callback is given, because readingactiveNetworkinside a callback can return the network being replaced.Network.Statususes the same function.connected=falseand thenconnected=true. A real disconnect is reported 1 s late.For review:
nativephp/mobile ^4.0, becauseBroadcastsGloballyexists only in v4.NativePHP\Network\Events\→src/Events/, as Proposal: Add reactive NetworkStatusChanged event #2 suggested.composer dump-autoload -oprints no PSR-4 warnings.status()on return.connectedis still "a default network exists", even behind a captive portal. UsingNET_CAPABILITY_VALIDATEDwould change existing behaviour, so it's left for a separate PR.Test plan
vendor/bin/pestagainstnativephp/mobile4.7.0: 42 passed.NetworkStatusChangedTestsends the native payload throughNative::test()->emitNative()and checks it reaches an#[On]handler andEvent::listen. Renaming a constructor parameter away from its payload key makes both tests fail.PluginTestnow checks the manifest event, both init functions, and that both native files send the declared event name.pint --testpasses.BridgeFunctionandLaravelBridge.android.jar(API 36) with stubs forBridgeFunctionandNativeElementBridge.cellular(isExpensive=true), thenwifi, with no offline event in between. Before the second commit, Wi-Fi off sentconnected=falseand thencellular18 to 56 ms later.connected=false, 1.35 s after the toggle.cellular, thenwifi3.4 s later. These match the system's two default-network switches.connected=false, then oneconnected=true type=ethernetwhen it came back, with no duplicates. The simulator can't switch to cellular, so that wasn't checked on iOS.