Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/filipchalupa/connection-state
Detect online and offline network state.
https://github.com/filipchalupa/connection-state
Last synced: about 15 hours ago
JSON representation
Detect online and offline network state.
- Host: GitHub
- URL: https://github.com/filipchalupa/connection-state
- Owner: FilipChalupa
- Created: 2023-05-10T13:03:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-05-10T15:59:25.000Z (over 1 year ago)
- Last Synced: 2024-10-31T11:01:10.794Z (7 days ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/connection-state
- Size: 389 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Connection state [![npm](https://img.shields.io/npm/v/connection-state.svg)](https://www.npmjs.com/package/connection-state) ![npm type definitions](https://img.shields.io/npm/types/connection-state.svg)
Detect online and offline network state.
You can see it in action here [filipchalupa.cz/connection-state](https://filipchalupa.cz/connection-state/). Try to disconnect your device from the internet after loading the page.
## Installation
```bash
npm install connection-state
```## Usage
```js
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.
```js
const connection = connectionState({
endpoints: ['https://www.google.com'],
})
```## Development
- Install dependencies: `npm ci`
- Run: `npm run dev`