Coder Social home page Coder Social logo

socialize-user-presence's Introduction

User Presence

This package is a simple, scalable package for keeping track of if your users are online, idle, or offline. While other packages exist that do this same thing, most of them fall flat when it comes to scalability and they lock you into keeping the status data on the profile key of the user record. This package cleans up neatly after an app instance dies and lets you configure what actions to take when the users presence changes state.

Server API

The server side API consists of methods which register callbacks to run when a users presence changes. A user is considered online if any session is set to online, idle if all sessions are set to idle, or offline if there are no current sessions for the user.

UserPresence.onSessionConnected(Fn(connection)) - register a callback to run each time a logged in user makes a connection to the server.

UserPresence.onSessionConnected(function(connection){
    Sessions.insert({_id:connection.id, userId:connection.userId});
});

UserPresence.onSessionDisconnected(Fn(connection)) - register a callback to run each time a logged in user breaks connection to the server.

UserPresence.onSessionDisconnected(function(connection){
    Sessions.remove(connection.id);
});

UserPresence.onCleanup(Fn()) - register a callback to run when your application starts fresh without any other instances running. T

UserPresence.onCleanup(function(){
    Meteor.users.update({}, {$unset:{status:true}}, {multi:true});
});

UserPresence.onUserOnline(Fn(userId, connection)) - register a callback to run when the users status is "Online" (Any one session is online).

UserPresence.onUserOnline(function(userId){
    Meteor.profiles.update({userId:userId}, {$set:{status:"online"}})
});

UserPresence.onUserIdle(Fn(userId, connection)) - register a callback to run when the users status is "Idle" (All sessions are idle)

UserPresence.onUserIdle(function(userId){
    Meteor.profiles.update({userId:userId}, {$set:{status:"idle"}})
});

UserPresence.onUserOffline(Fn(userId, connection)) - register a callback to run when the users status is "Offline" (No connected sessions)

UserPresence.onUserOffline(function(userId){
    Meteor.profiles.update({userId:userId}, {$unset:{status:true}})
});

User Extensions

This package provides some extensions onto the User class which comes with socialize:user-model for your convenience.

User.prototype.setStatusIdle() - Set the current logged in user for this session to idle.

Meteor.user().setStatusIdle();

User.prototype.setStatusOnline() - Set the current logged in user for this session to online.

Meteor.user.setStatusOnline();

socialize-user-presence's People

Contributors

copleykj avatar dan335 avatar ibrahimzahoor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.