Detecting Whether a User Is Online
Preface
How to determine if a user is online, rather than just whether the network is connected.
Online: connected to a network and able to access the internet.
Connected: connected to a network but unable to access the internet.
Detecting Online Status
As mentioned above, to determine if a user is online, you need to check whether the internet is accessible.
Node.js
In Node.js, you can ping some common domains to determine online status.
Here is a utility for this: https://code.insistime.com/#/qiao-ping
Usage:
// use
const res = await ping('insistime.com');
// res
{
alive: true,
avg: '3.177',
host: 'insistime.com',
inputHost: 'insistime.com',
max: '3.177',
min: '3.177',
numeric_host: 'xx',
output: '',
packetLoss: '0.000',
stddev: '0.000',
time: 3.177,
times: [
3.177,
],
}
This way you can use ping to determine online status.
By pinging several of the most common domains, we can determine whether the user is online.
Here is a utility for this: https://code.insistime.com/#/qiao-is-online
Usage:
// use
const res = await isOnline();
// res
'online' or 'offline'
Browser
The browser environment doesn’t have ping capability, so we detect online status by loading an image instead.
Code:
/**
* isOnline
* @param {*} src
* @returns
*/
export const isOnline = (src) => {
return new Promise(function (resolve, reject) {
if (!src) return reject(Error("need online img src"));
let img = new Image();
img.src = `${src}?r=${Math.random()}`;
img.onload = () => {
resolve("online");
};
img.onerror = () => {
resolve("offline");
};
});
};
Here is a utility for this: https://code.insistime.com/#/qiao-is-online-browser
Usage:
// use
const res = await isOnline('img url');
// res
'online' or 'offline'
Detecting Offline-to-Online Transition
Now that we can detect online status,
there’s a scenario where you need to detect when a user transitions from offline back to online.
The approach is:
Periodically check online status. After multiple offline results followed by several consecutive online results,
consider the user to have transitioned from offline to online.
Here is a utility for this: https://code.insistime.com/#/offline-to-online
Usage:
|—Node.js
const { offlineToOnline } = require("qiao-is-online");
// callback
// time, interval time, default is 3*1000ms
offlineToOnline(function () {
console.log("offline-to-online");
}, 3 * 1000);
|—Browser
const { offlineToOnline } = require("qiao-is-online-browser");
// is online img src
const isOnlineImgSrc = "your online img src";
// callback
// time, interval time, default is 3*1000ms
offlineToOnline(
isOnlineImgSrc,
function () {
console.log("offline-to-online");
},
3 * 1000
);
Summary
-
Ping in Node.js: https://code.insistime.com/#/qiao-ping
-
Online detection in Node.js: https://code.insistime.com/#/qiao-is-online
-
Online detection in browser: https://code.insistime.com/#/qiao-is-online-browser
-
Offline-to-online detection for Node.js and browser: https://code.insistime.com/#/offline-to-online
Related Articles
Getting Started with Mantine UI
Preface: In 2023, which UI framework should you use for frontend development? Key requirements: PC and mobile support, dark/light theme switching, rich components, and React support. After comparing React UI libraries on GitHub, Mantine UI stands out for its extensive component library.
Using npm Workspaces for Frontend Monorepos
Migrating from lerna bootstrap to npm workspaces for monorepo dependency management — comparing lerna.json vs package.json config, node_modules location, and local package linking.
Initializing a Frontend Monorepo Project
Preface: This article documents the process of initializing a frontend Monorepo project, covering LICENSE, git configuration, package.json, commitizen, commitlint, rollup, ava, lerna, nx, prettier, eslint, and lint-staged.
A Lightweight JS Testing Framework: AVA
Common JS testing frameworks include Jest, Mocha, etc. This article introduces a lightweight JS testing framework: AVA.
Nx VS Lerna
Comparing NX and Lerna for monorepo management — task execution, local caching, distributed caching, dependency visualization, version management, and npm publishing.