BBVAEngineering / ember-network-state

Check and react on network state of your progressive web app
MIT License
21 stars 4 forks source link

ember-network-state

Build Status GitHub version NPM version Dependency Status codecov Greenkeeper badge Ember Observer Score FOSSA Status

Information

NPM

The browser provides network property window.navigator.onLine and events online and offline. The problem is that this API is not reliable, we can have an interface connection (phone is not on airplane mode, we have WiFi data) but the network may not have access to the internet.

In order to confirm the connection status, this addon pings a URL (by default it is the favicon.ico) when it detects that user supposedly have connectivity again. When the ping ends and it goes OK, it will switch to online state, but if ping doesn't go OK, this addon will keep the state limited and it will schedule a ping every certain time.

If the browser has implemented connection API, it will listen for changes on network quality as well.

Below is an example of all states of the service:

States

Usage

Install the addon with ember-cli.

ember install ember-network-state

Inject the service in your app:

export default class MyClass {
  @service network;
}

Interface

Properties

Methods

Events

You can subscribe to the change event to receive changes on state property.

const network = this.get('network');

network.on('change', (state) => {});

Configuration

The addon can be configured in config/environment.js of your app.

module.exports = function (/* environment */) {
  return {
    'network-state': {
      reconnect: {
        auto: true,
        path: '/favicon.ico',
        delay: 5000,
        multiplier: 1.5,
        timeout: 15000,
        maxDelay: 60000,
        maxTimes: -1,
      },
    },
  };
};

Posible values:

Contribute

If you want to contribute to this addon, please read the CONTRIBUTING.md.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See the list of contributors who participated in this project.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

FOSSA Status