Skip to content

@webframe/inactivity-timeout ​

A timeout that runs once nothing has happened for a while: an idle socket to close, or a user to sign out. Call reset whenever something happens. It works in Node and in browsers.

sh
npm install @webframe/inactivity-timeout
ts
import { createInactivityTimeout } from '@webframe/inactivity-timeout'

const timeout = createInactivityTimeout({
  onTimeout: () => socket.end(),
  timeoutInMilliseconds: 60_000,
})

socket.on('data', () => timeout.reset())
socket.on('close', () => timeout.clear())
timeout.start()

Options ​

OptionWhat it does
onTimeoutCalled once the timeout is reached.
timeoutInMillisecondsHow long nothing has to happen. 30 seconds by default.

What it returns ​

NameWhat it does
start()Starts the timeout. Throws if it's already started.
reset()Starts it again from now. Throws if it isn't started. After it has run, reset starts it again.
clear()Stops it. Throws if it isn't started.
idThe timer once started (even after it has run), until it's cleared; otherwise null.

Released under the MIT Licence.