npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

primus-users

v0.1.1

Published

Adds users to Primus

Downloads

11

Readme

Primus Users plugin

THIS IS A WORKING VERSION - tests to be added.

Users plugin is an extension for Metroplex and Omega Supreme Primus's plugins. It associates sparks and servers with the users.

Q: Why would I use a plugin for a plugin?

A: Because it makes sense in this case. Users plugin uses both Metroplex and Omega Supreme's functionalities and extends them.

Installation

You can install the module from NPM:

npm install --save primus-users

Once you've installed the module you can tell Primus to use the plugin:

'use strict';

var http = require('http').createServer(),
    Primus = require('primus'),
    metroplex = require('metroplex'),
    omegaSupreme = require('omega-supreme'),
    usersPlugin = require('primus-users'),
    primus = new Primus(http, { transformer: 'uws' });
  
// first add Omega Supreme plugin
primus.plugin('omega-supreme', omegaSupreme);
// then add Metroplex plugin
primus.plugin('metroplex', metroplex);
// to finally add Users plugin
primus.use('users', usersPlugin);

Usage

There is only one (optional) option you can use with Users plugin:

  • uidKey: the key which will hold user's id (default is 'uid') in the request. This can be done (as an example) on primus authorization. The key should be added to the requests like this:
var uid = 10;
function onAuthorize (request, done) {
  request[this.options.uidKey] = uid;
  done(!user ? new Error('No user') : void 0);
}

This option can be provided in the options object of the Primus server:

primus = new Primus(http, {
  transformer: 'sockjs',
  uidKey: 'some_other_property_than_uid'
});

Users plugin methods

User plugin exposes primus.users with following public methods available:

users.isOnline

users.isOnline(uid, fn)

Checks whether a user is online.

users.isOnline(123, function (err, isOnline) {
  console.log(isOnline);
});

users.areOnline

users.areOnline(uids, fn)

Checks whether the users are online.

users.areOnline([ 123, 234 ], function (err, areOnline) {
  console.log(areOnline);
});

users.allUsers

users.allUsers(fn)

Get all current registered users.

users.allUsers(function (err, allUsers) {
  console.log(allUsers);
});

users.serverUsers

users.serverUsers(fn)

Get all current registered users associated with this server.

users.serverUsers(function (err, serverUsers) {
  console.log(serverUsers);
});

users.countAllUsers

users.countAllUsers(fn)

Count all current registered users.

users.countAllUsers(function (err, allUsersCount) {
  console.log(allUsersCount);
});

users.countServerUsers

users.countServerUsers(fn)

Count all current registered users associated with this server.

users.countServerUsers(function (err, serverUsersCount) {
  console.log(serverUsersCount);
});

Events

primus.users emits following public events:

online
users.on('online', fn)

Indicates that the user went online. This event is emitted only for the first connection of the spark associated to the user id.

users.on('online', function (uid) {
  console.log(uid);
});
offline
users.on('offline', fn)

Indicates that the user went offline. No spark sessions associated to the user id are connected to the server anymore.

users.on('offline', function (uid) {
  console.log(uid);
});

Metroplex integration

Users plugin benefits from Metroplex Redis sparks/connections registry. The Users plugin stores extra data in the database, so the connections can be associated with the user ids. This gives you an information such as user online status. That also means, that you can have this information from the outside of the process, because it's stored in the database.

Omega Supreme integration

Users plugin adds few additional methods to Omega Supreme's primus.forward:

forward.users

forward.users(uids, msg, fn)

Send a message to the users.

forward.users([ 5, 17, 234 ], { data: 1 }, function (err, result) {
  console.log(result);
});

forward.user

forward.user(uid, msg, fn)

Send a message to a user.

forward.user(5, { data: 1 }, function (err, result) {
  console.log(result);
});

License

MIT