Is it possible to count online time
-
I added two new hooks to make this easier.
https://github.com/NodeBB/NodeBB/commit/fcb10ebdbbb00d5a7e49b5529eec7be258d28d8d
You can use these hooks in a plugin to detect when a user connects and disconnects and increase their online time. Here is some sample code.
myPlugn.actionSocketsConnect = async function (hookData) { if (hookData.socket.uid > 0) { await user.setUserField(hookData.socket.uid, 'startTime', Date.now()); } }; myPlugn.actionSocketsDisconnect = async function (hookData) { if (hookData.socket.uid > 0) { let startTime = await user.getUserField(hookData.socket.uid, 'startTime'); startTime = parseInt(startTime, 10) || Date.now(); const newValue = await user.incrementUserFieldBy(hookData.socket.uid, 'onlinetime', Date.now() - startTime); await db.sortedSetAdd('users:onlinetime', newValue, hookData.socket.uid); } };
This is increment the users online time when they disconnect and also create a sorted set to quickly get the users sorted by their online time value.
Copyright © 2025 NodeBB | Contributors