ostrio:user-status

v2.0.0Published 2 years ago

support support

Meteor user-status

Reactive user's online, offline, and idle status updates. Current user's status stored in Meteor.user().profile.status.online, returns Boolean value.

Install:

meteor add ostrio:user-status

ES6 Import:

1import { UserStatus } from 'meteor/ostrio:user-status';

API

new UserStatus({opts}) constructor accepts couple of options:

  • opts {Object}
  • opts.throttleTiming {Number} — time in milliseconds between consecutive method calls; Default: 2048
  • opts.idleTimeout {Number} — timeout in milliseconds to consider user status idle; Default: 30000
1import { UserStatus } from 'meteor/ostrio:user-status';
2const userStatus = new UserStatus({ idleTimeout: 5000 });
  • userStatus.status {ReactiveVar} — Returns String, one of: offline, online, or idle
  • userStatus.stop() {Function} — Stop tracking user's status
  • userStatus.start() {Boolean} — Start tracking user's status, called upon constructor initialization
  • userStatus.isRunning {Boolean}

Usage

Use it as it is on the Client or with accounts-* packages. When used with Accountsostrio:user-status will update user's record in a MongoDB with its current status. Package work silently in the background and doesn't require any setting up, just initialize constructor.

Once initialized — use userStatus.status ReactiveVar to get current user's status in front end.

1import { Meteor } from 'meteor/meteor';
2import { UserStatus } from 'meteor/ostrio:user-status';
3
4// INITIATE UserStatus ON CLIENT/BROWSER
5// TO BUILD FONT END LOGC AROUND online AND idle STATUSES
6if (Meteor.isClient) {
7  const userStatus = new UserStatus();
8
9  // [Reactive] get current user's status, one of — offline, online, idle
10  userStatus.status.get();
11
12  // Stop tracking user's status on the client (browser)
13  userStatus.stop();
14
15  // Start tracking user's status on the client (browser),
16  // without creating `new UserStatus()` instance
17  userStatus.start();
18}
19
20// OPTIONALLY INITIATE UserStatus ON SERVER
21// THIS IS REQUIRED ONLY TO UPDATE USER'S OBJECT IN MONGODB
22if (Meteor.isServer) {
23  // NOTE: WHEN INITIALIZED ON SERVER
24  // LOGIN/LOGOUT HOOKS APPLIED TO ALL USERS
25  const userStatus = new UserStatus();
26
27  // Stop tracking user's status on the Server
28  userStatus.stop();
29
30  // Re-start tracking user's status on the Server
31  userStatus.start();
32
33  // NOTE: CALLING `new UserStatus()` TWICE
34  // ON THE SERVER WILL THROW AN EXCEPTION!
35}

Updated user's object

1{
2  connection: String, //-> Current or last used DDP Connection ID
3  profile: {
4    status: {
5      online: Boolean, //-> Is user online
6      idle: Boolean,   //-> Is user online but idle
7      lastLogin: Date, //-> Current or last login time
8      lastSeen: Date   //-> Last Date when user has changed its status
9    }
10  }
11};

Idle Status

Why idle?: Some apps require your constant attention. Such apps often include games, media players, anything that is CPU/battery intensive, and so on. For these kinds of apps, it may be important (as well as user-friendly) to do something when a user is no longer actively interacting with your app.

Why online and last seen?: Some apps like chats, forums and other social platforms may require to show user's (on|off)line status. If user is offline at the moment such apps require to show the last date when user was active.

Why track connection id?: Established DDP connection is user-less, to find right user from database we're using DDP's connection id.

Support this project: