Skip to content

application.network ​

application.network represents the device's network status at this moment: whether it is connected to a network, which kind of network it uses, and whether that network is metered. When any of the three changes, it fires the change event.

There is only one application.network in the whole app, and it can be used at any time, including at the top level of a script. Its class is Network. All three properties are read-only, and each read gives the status at that moment.

"Connected" means the device has a network that can be used to establish connections. It does not guarantee that any particular server can be reached: while isConnected is true, fetch() can still fail, and when it does, the Promise returned by fetch() is rejected.

In the simulator, the network status follows the Mac running the simulator.

Inheritance: EventTarget → Network

Constructor ​

Cannot be created with new: new Network() throws TypeError with the message Illegal constructor: Network is created by the runtime, not by new. The only instance is obtained from the network property of application.

Instance properties ​

PropertyDescription
isConnected read-onlyWhether a network is connected
type read-onlyWhich kind of network is in use: 'wifi', 'cellular', 'wired', 'other', or 'none' when not connected
isExpensive read-onlyWhether the network is metered, such as cellular data or a personal hotspot

Instance methods ​

Inherits addEventListener(), removeEventListener(), and dispatchEvent() from EventTarget, used to listen for the event below.

Events ​

EventWhen it fires
changeOne of isConnected, type, or isExpensive changed

change is the only event application.network fires. Listening for any other name does not throw: it is treated as a custom event and fires only when you dispatch it yourself with dispatchEvent().

Examples ​

Network status involves no UI, so this code runs as is at the top level of main.js.

js
const network = application.network;
console.log('Connected:', network.isConnected, 'Type:', network.type, 'Metered:', network.isExpensive);

network.addEventListener('change', () => {
  console.log('Network changed, now', network.type);
});

Result

  • At launch, one line with the current status is printed. On the simulator used for acceptance, it reads Connected: true Type: wifi Metered: false.
  • Later, when the network goes down, comes up, or switches, Network changed, now is printed with the new type. Not verified in the simulator.

Corresponding iOS API ​

On iOS, network status comes from nw_path_monitor in the Network framework (NWPathMonitor in Swift). It is not an ObjC class; Network and its members are defined by Nacre.

iOSJS
nw_path_monitor_create(), nw_path_monitor_start()Not needed. Read application.network directly
nw_path_get_status(path) == nw_path_status_satisfiednetwork.isConnected
nw_path_uses_interface_type(path, nw_interface_type_wifi) and so onnetwork.type
nw_path_is_expensive(path)network.isExpensive
nw_path_monitor_set_update_handler()network.addEventListener('change', …)

Comparison with the browser ​

This table is only for people familiar with the web to look up correspondences; the rest of the page does not depend on it.

Webapplication.network
navigator.onLineisConnected
navigator.connection.typetype
The online and offline eventsThe change event; read isConnected in the callback

See also ​

  • application: network is one of its properties
  • fetch(): sending requests. A request can still fail while isConnected is true
  • WebSocket: a persistent connection has its own close event when it drops