connection-state
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Connection state npm npm type definitions

Detect online and offline network state.

You can see it in action here filipchalupa.cz/connection-state. Try to disconnect your device from the internet after loading the page.

Installation

npm install connection-state

Usage

import { connectionState } from 'connection-state'

const connection = connectionState()

console.log(`You are ${connection.getState()}.`)
// connection.getState() returns 'online' or 'offline'

connection.addListener((state) => {
	console.log(`Your connection state has changed. You are now ${state}.`)
})
// state is 'online' or 'offline'

Endpoints

You can specify endpoints to check if you are online. The library will try to fetch the endpoints. If it succeeds, you are online. If it fails, you are offline.

const connection = connectionState({
	endpoints: ['https://www.google.com'],
})

Development

  • Install dependencies: npm ci
  • Run: npm run dev

Package Sidebar

Install

npm i connection-state

Weekly Downloads

116

Version

0.1.0

License

ISC

Unpacked Size

32.3 kB

Total Files

8

Last publish

Collaborators

  • onset