Skip to content

Send NetworkStatusChanged when the network status changes - #4

Open
wojt-janowski wants to merge 3 commits into
NativePHP:mainfrom
wojt-janowski:feature/network-status-changed
Open

wojt-janowski wants to merge 3 commits into
NativePHP:mainfrom
wojt-janowski:feature/network-status-changed

Conversation

@wojt-janowski

@wojt-janowski wojt-janowski commented Nov 12, 2026 •

Copy link
Copy Markdown

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's onResume(). 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 in nativephp.json events. The properties are the four status() fields.
  • iOS: Answer Network.Status on iOS from one long-lived path monitor #3's long-lived NWPathMonitor now 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 small NetworkStatus struct, so Network.Status and the event build the same payload.
  • Android: registerDefaultNetworkCallback on the main looper, started from networkInit(context), which is safe to call more than once. The status is computed from the capabilities each callback is given, because reading activeNetwork inside a callback can return the network being replaced. Network.Status uses the same function.
  • The launch status is the baseline and isn't sent, and repeats are dropped.
  • On Android a lost connection is reported after 1 s, and not at all if another network becomes the default first. When Wi-Fi turns off, Android reports the loss about 20 ms before it makes mobile data the default, so without the delay every handover sent connected=false and then connected=true. A real disconnect is reported 1 s late.
  • README and Boost guideline: a "Reacting to changes" section. The "No event system" note is replaced.

For review:

  • Answering Proposal: Add reactive NetworkStatusChanged event #2's question: this requires nativephp/mobile ^4.0, because BroadcastsGlobally exists only in v4.
  • Composer autoload gains NativePHP\Network\Events\ → src/Events/, as Proposal: Add reactive NetworkStatusChanged event #2 suggested. composer dump-autoload -o prints no PSR-4 warnings.
  • An event that arrives while no screen is live (the app is in the background) isn't replayed. The docs say to call status() on return.
  • Android connected is still "a default network exists", even behind a captive portal. Using NET_CAPABILITY_VALIDATED would change existing behaviour, so it's left for a separate PR.

Test plan

  • vendor/bin/pest against nativephp/mobile 4.7.0: 42 passed. NetworkStatusChangedTest sends the native payload through Native::test()->emitNative() and checks it reaches an #[On] handler and Event::listen. Renaming a constructor parameter away from its payload key makes both tests fail. PluginTest now checks the manifest event, both init functions, and that both native files send the declared event name.
  • pint --test passes.
  • Swift: typechecked against the iOS 18.2 simulator SDK (Xcode 27.2, Swift 5 mode) with stubs for BridgeFunction and LaravelBridge.
  • Kotlin: compiled against android.jar (API 36) with stubs for BridgeFunction and NativeElementBridge.
  • Device check, Android 17 emulator (API 37, Pixel 9 Pro profile, Wi-Fi plus LTE), showcase app built from this branch with a probe screen:
    • Cold launch: no event.
    • Wi-Fi off, then on, done twice: exactly cellular (isExpensive=true), then wifi, with no offline event in between. Before the second commit, Wi-Fi off sent connected=false and then cellular 18 to 56 ms later.
    • Airplane mode on: one connected=false, 1.35 s after the toggle.
    • Airplane mode off: cellular, then wifi 3.4 s later. These match the system's two default-network switches.
  • Device check, iOS: iPhone 17 Pro simulator (Xcode 27.2). Cold launch: no event. Taking the host's only network link down for 15 s sent one connected=false, then one connected=true type=ethernet when it came back, with no duplicates. The simulator can't switch to cellular, so that wasn't checked on iOS.

ngunyimacharia and others added 2 commits September 28, 2026 12:53
…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
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>
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.

Proposal: Add reactive NetworkStatusChanged event

2 participants