veliovgroup / meteor-user-status

Reactively track user's online, offline, and idle statuses

Home Page:https://atmospherejs.com/ostrio/user-status

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

import { 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
import { UserStatus } from 'meteor/ostrio:user-status';
const 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.

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

// INITIATE UserStatus ON CLIENT/BROWSER
// TO BUILD FONT END LOGC AROUND online AND idle STATUSES
if (Meteor.isClient) {
  const userStatus = new UserStatus();

  // [Reactive] get current user's status, one of — offline, online, idle
  userStatus.status.get();

  // Stop tracking user's status on the client (browser)
  userStatus.stop();

  // Start tracking user's status on the client (browser),
  // without creating `new UserStatus()` instance
  userStatus.start();
}

// OPTIONALLY INITIATE UserStatus ON SERVER
// THIS IS REQUIRED ONLY TO UPDATE USER'S OBJECT IN MONGODB
if (Meteor.isServer) {
  // NOTE: WHEN INITIALIZED ON SERVER
  // LOGIN/LOGOUT HOOKS APPLIED TO ALL USERS
  const userStatus = new UserStatus();

  // Stop tracking user's status on the Server
  userStatus.stop();

  // Re-start tracking user's status on the Server
  userStatus.start();

  // NOTE: CALLING `new UserStatus()` TWICE
  // ON THE SERVER WILL THROW AN EXCEPTION!
}

Updated user's object

{
  connection: String, //-> Current or last used DDP Connection ID
  profile: {
    status: {
      online: Boolean, //-> Is user online
      idle: Boolean,   //-> Is user online but idle
      lastLogin: Date, //-> Current or last login time
      lastSeen: Date   //-> Last Date when user has changed its status
    }
  }
};

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:

About

Reactively track user's online, offline, and idle statuses

https://atmospherejs.com/ostrio/user-status

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:JavaScript 100.0%