As part of general Open Source org housekeeping, and due to lack of interest in this project, we have decided to archive NetworkJS.
Network JS is a utility library that emits network connectivity events.
Min Node Version. 12.16.1
yarn add [email protected]:tophat/networkjs.git
or
npm install [email protected]:tophat/networkjs.git
import Network from 'network-js'
If you're not using ECMAScript modules:
const Network = require('network-js').default
Then initialize the library with your preferred configuration described below.
const Net = new Network({
service: { /* service config */ },
stability: { /* stability config */ }
})
This library monitors for three types of network events:
offline
: Fired when the browser loses network connectiononline
: Fired when the browser reconnects
unstable
: Fired when the network speed goes under a given thresholdstable
: Fired when the network speed goes back above the given threshold
maxBufferSize
: The number of recent performance entries to track at any given time (optional, default 10)speedThreshold
: The speed threshold (in KBps) that determines network stability (optional, default 100)
Avoid using a large value for maxBufferSize
(>100) as performance entries are stored in memory. The smaller the number you provide, the less accurate the monitor may be. The larger the number you provide, the longer it will take to propagate changes in network stability. You should play around with this value until you find a good balance.
This monitor uses the window.PerformanceObserver
API which is not supported in Internet Explorer. As such, this monitor will be disabled in Internet Explorer.
degraded
: Fired when a service that matches a given prefix becomes degradedresolved
: Fired when a service degradation is resolved
definitions
: Array of service definitions to track (optional, defaults to tracking any failures)
name
: Name of the serviceregex
: TheRegExp
to test against
statuses
: Array of statuses that determine a service degradation (optional, default [502, 503, 504])failureThreshold
: Minimum number of consecutive failures that determine if a service is degraded (optional, default 2)decrementTime
: Amount of time until a failure is dismissed (optional, default 10000ms)
This monitor needs to be hooked into your current HTTP library. For each request, you feed it the request URL and the response status code, and it will emit events upon hitting the given failure threshold.
const Net = new Network({ service = { /* service config */ } })
this.axios.interceptors.response.use(
success => success,
(error) => {
Net.serviceError(error.requestUrl, error.status)
return error
},
)
const Net = new Network()
Net.on('online', () => {
console.log('Network - ONLINE')
})
Net.on('offline', () => {
console.log('Network - OFFLINE')
})
const Net = new Network({
stability: {
maxBufferSize: 15,
speedThreshold: 150
}
})
Net.on('unstable', () => {
console.log('Network - UNSTABLE')
})
Net.on('stable', () => {
console.log('Network - STABLE')
})
const Net = new Network({
services: {
definitions: [
{
name: 'resource 1',
regex: new RegExp('api/v1/resource1')
},
{
name: 'resource 2',
regex: new RegExp('api/v2/resource2')
}
],
statuses: [500, 502, 503, 504]
}
})
Net.on('degraded', (serviceName) => {
console.log(`Network - ${serviceName} - DEGRADED`)
})
Net.on('resolved', (serviceName) => {
console.log(`Network - ${serviceName} - RESOLVED`)
})
const Net = new Network({
stability: {
maxBufferSize: 15,
speedThreshold: 150
},
services: {
prefixes: ['api/v1/resource1', 'api/v2/resource2'],
statuses: [500, 502, 503, 504]
}
})
Net.all((event) => {
console.log(`Network - ${event}`)
})
See Contributing Guide to get started contributing.
Thanks goes to these wonderful people (emoji key):
Jack Cohen π€ π» |
Marc Cataford π |
StealthGiraffe π» |
Anthony Sottile π‘οΈ |
This project follows the all-contributors specification. Contributions of any kind welcome!