node package manager


Monitor a URI's connectivity

:toc: macro :toc-title: :toclevels: 9

URI Monitor

image:["Circle CI", link=""]


npm install --save uri-monitor

Basis for following examples:

import Monitor from 'uri-monitor'
const monitor = Monitor.create('', 1500)

Monitoring outages:

const serviceChanges = monitor.filter(({ type }) => type === 'change')
.filter({ data: { isResponsive }} => !isResponsive)
.observe(({ data: { result }}) => console.warn('Service Down!', result))
.filter({ data: { isResponsive }} => isResponsive)
.observe(() => console.warn('Service Up!'))

Monitoring outages via <<convenience-streams-on-stream, Stream sugar>>:

.observe(() => console.warn('Service Down!'))
.observe(() => console.warn('Service Up!'))
create :: String, Maybe Integer -> Stream MonitorEvent
--        uri     interval
  • uri The URI that periodic GET requests will be made to.

  • interval Optional, defaults to 1000. The milliseconds between each request. Note this is not the time between a response and the next request because requests are evenly staggered. For example:

--- = Time    Q = Request    R = Response
^        ^        ^        ^    <== Evenly staggered

A response cannot temporally leak into the next request because each request is configured to timeout within the interval.

The following will cause the result of a request to be considered a failure:

  • Request is not responded to within interval
  • Response HTTP Status Code is >= 400.
  • Any general HTTP Error (e.g. a client-side CORS error)

In all of these cases data.isResponsive will be false and data.result will be either a <<requesterror, RequestError>> or <<networkerror, NetworkError>>.

The <<stream,Stream>> returned by <<create, create>> is monkey patched with filtered variants for common patterns. They are:

pongs :: Stream CheckedEvent

<<checkedevent, CheckedEvents>> where data.isResponsive is true.

drops :: Stream CheckedEvent

<<checkedevent, CheckedEvents>> where data.isResponsive is false.

ups :: Stream ChangedEvent

<<changedevent, ChangedEvents>> where data.isResponsive is true.

ups :: Stream ChangedEvent

<<changedevent, ChangedEvents>> where data.isResponsive is false.

An instance of link:[most].

Base interface that all emitted events implement. If isResponsive is false then result is either a <<requesterror, RequestError>> or <<networkerror, NetworkError>> otherwise result is a <<response, Response>>.

type : String
data :
  isResponsive: Bool
  result: CheckResult

Emitted every time a result of a request is available.

type: "checked"
  isResponsive: Bool
  result: CheckResult

Emitted every time a result of a request has a different isResponsive value than the previous result.

type: "changed"
  isResponsive: Bool
  result: CheckResult
RequestError | NetworkError | Response

A link:[Superagent Response].

message: String
status: Integer
body: Object
res: Response
message: String
originalError: Object