Coder Social home page Coder Social logo

meteor-user-presence's Introduction

USER PRESENCE

This package monitors the user to track user's state and save 3 fields in user's record:

  • statusDefault - Status setted by user
  • statusConnection - Connection status (offline, online and away)
  • status
    • Offline if statusConnection or statusDefault are offline
    • Same as statusConnection if statusDefault is online
    • Same as statusDefault

How to use

Add package

meteor add konecty:user-presence

Configure client

//CLIENT
Meteor.startup(function() {
	// Time of inactivity to set user as away automaticly. Default 60000
	UserPresence.awayTime = 300000;
	// Set user as away when window loses focus. Defaults false
	UserPresence.awayOnWindowBlur = true;
	// Start monitor for user activity
	UserPresence.start();
});

Start server

//SERVER
// Listen for new connections, login, logoff and application exit to manage user status and register methods to be used by client to set user status and default status
UserPresence.start();
// Active logs for every changes
// Listen for changes in UserSessions and Meteor.users to set user status based on active connections
UserPresenceMonitor.start();

Logs

//SERVER
UserPresence.activeLogs();

Server Methods

// Create a new connection, this package do this automaticly
Meteor.call('UserPresence:connect');
// Set connection as away, can be usefull call this method if you are using cordova to ser user as away when application goes to background for example.
Meteor.call('UserPresence:away');
// Set connection as online
Meteor.call('UserPresence:online');
// Changes the default status of user
Meteor.call('UserPresence:setDefaultStatus', 'busy');

meteor-user-presence's People

Contributors

distlibs avatar elie222 avatar engelgabriel avatar ggazzo avatar kirbyssmith avatar marcosspessatto avatar paulincai avatar rodrigok avatar sakulstra avatar sampaiodiego avatar shiqimei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meteor-user-presence's Issues

Using this package when running multiple instances

I've been reading through the code for this package and I just realised that the following code:
UserPresenceMonitor.start();
should only be called once. Is that correct? I have 40 or so servers oberving this info when one would be enough! If this is true, the docs need to be updated to be more clear on this.

UserID as parameter for Methods? Use this.userId instead at server side?

Is there a reason you are passing the UserID from the client to the server in the methods? It seems this would be a security issue (not major since it's not able to access a lot of data), but would allow a user to spoof any user is online/offline by passing their userId to the method. It seems the method at the server side should just use this.userId to determine the userId instead.

recommended way to display visitor counts

Hello, i'd like to know what's the recommended way to display a visitor count in our application.

I've seen #20 but i'm not sure if i got the suggested solution, it would be nice if someone could elaborate.

Best regards,
lukas

How should it be used?

There is no documentation yet, And the code looks great! How should it be used? I'm trying to replace a bad user presence package. Thanks a lot.

Startup methods breaks connection to server in development mode.

With these two methods in the Meteor.startup function, you have to refresh the browser whenever the meteor server is reloaded in development mode. It prevents any method call or Meteor.subscription to work.

UserPresence.start();
UserPresenceMonitor.start();

Register Instance name unique?

I've two servers running the same app, do I have to add unique names to each one? I see user online connected to both servers but one user doesn't see the other as online.

UserPresence.userId

Is it safe to set userId in meteor call?

Meteor.call('UserPresence:online', UserPresence.userId);

Why you need to send a userId and you do not using Meteor.userId() on server?

Thanks a lot.

Performance issue with large user collection.

After loading 180,000 users the application will no longer start. The issue seems to be a related to the following code found in the UserPresenceMonitor.start function in monitor.js. After removing this code the application started working again.

Meteor.users.find({}).observeChanges({
   changed: UserPresenceMonitor.processUser
});

This seems to be a costly operation as it observes the entire user collection.
When I dug deeper it saw that processUser returns immediately unless the statusDefault field changed. If statusDefault is changed then the function calls UserPresenceMonitor.processUserSession().

processUser: function(id, fields) {
   if (fields.statusDefault == null) {
      return;
   };
   var userSession = UsersSessions.findOne({_id: id});
   if (userSession) {
      UserPresenceMonitor.processUserSession(userSession, ''changed'');
   };
}

Processing the user’s session updates the user’s status and statusConnection. Given that UserPresence.setDefaultStatus() already sets the user’s status and statusConnection is not impacted by changing statusDefault, is this observation necessary?

Related information:
Project : RocketChat
meteorVersion: 1.2.1
nodeVersion : 0.10.42

UserPresenceMonitor.onSetUserStatus triggering much more frequently than expected

We have console.logs inside this callback that are trigger log velocity warnings sometimes over 25k lines of log messages within a minute. Our server has 12 cores and we are using meteorhacks:cluster. As far as I can tell we have correctly set up meteor-multiple-instances-status as directed. It's working as expected but I'm concerned about all the excess activity in the logs. Most of the log messages show that the user's status hasn't actually changed.

Here's a small sample from our logs

 Updating user status for User: ggXFMHvthnTTGQj6v
 Previous status: online
 new status: online
 status connection: online
 User is online so clearing users timeout warning
 Updating user status for User: ggXFMHvthnTTGQj6v
 Previous status: online
 new status: online
 status connection: online
 User is online so clearing users timeout warning
 Updating user status for User: ggXFMHvthnTTGQj6v
 Previous status: online
 new status: online
 status connection: online
 User is online so clearing users timeout warning
 Updating user status for User: ggXFMHvthnTTGQj6v
 Previous status: online
 new status: online
 status connection: online
 User is online so clearing users timeout warning
 Updating user status for User: ggXFMHvthnTTGQj6v
 Previous status: online
 new status: online
 status connection: online
 User is online so clearing users timeout warning
 Updating user status for User: ggXFMHvthnTTGQj6v
 Previous status: online
 new status: online

As you can see here all of these triggered for the same user and the status wasn't even changing. Sometimes we have thousands of lines like this all for the same user in our logs.

Bug: User status does not update in "UserPresence:online"

Description:
When a user is making an active usage of the R.C App, user's status should be change in the UserPresence.setConnection, which is being called from the event "UserPresence:online".

Altough, the status does not change due to a wrong update query.

image

The status will be change only for user's that have a statusDefault: "online".
Which means that if a user is offline, and does not have a statusDefault, he'll stay "offline" while he should be "online".

The query should be:
image

user stays permanently online

I often see users online for days, without being logged in to the system.
In the usersSession collection, there are still entries for session, which do not exist anymore...

I do experience that issue with Rocket.Chat, but I did not manage to find out, under which circumstances this happens.

memory leak?

Just seen this error that caused the app to stop on localhost. Any ideas what this is?

I sometimes see my production instances go down after a few weeks running and wondering if it might be this issue. I've had these issues for a while, but never found the error.

W20160203-18:47:10.089(2)? (STDERR) (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
W20160203-18:47:10.131(2)? (STDERR) Trace
W20160203-18:47:10.132(2)? (STDERR) at process.addListener (events.js:160:15)
W20160203-18:47:10.133(2)? (STDERR) at process.on.process.addListener (node.js:802:26)
W20160203-18:47:10.134(2)? (STDERR) at Object.InstanceStatus.registerInstance (packages/konecty_multiple-instances-status/konecty_multiple-instances-status.js:50:1)
W20160203-18:47:10.134(2)? (STDERR) at Object.InstanceStatus.ping (packages/konecty_multiple-instances-status/konecty_multiple-instances-status.js:102:1)
W20160203-18:47:10.135(2)? (STDERR) at packages/konecty_multiple-instances-status/konecty_multiple-instances-status.js:79:1
W20160203-18:47:10.135(2)? (STDERR) at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
W20160203-18:47:10.137(2)? (STDERR) at packages/meteor/timers.js:6:1
W20160203-18:47:10.137(2)? (STDERR) at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)

Error on process.exit(0). Meteor code must always run within a Fiber.

Trying to run some CI jobs using dispatch:mocha-phantomjs with Meteor 1.3. When dispatch:mocha-phantomjs tries to exit with process.exit(0), this error always occurs:

W20160602-18:45:01.931(-7)? (STDERR)
W20160602-18:45:01.931(-7)? (STDERR) /tmp/meteor-test-run1iqje6z/.meteor/local/build/programs/server/packages/meteor.js:1060
W20160602-18:45:01.931(-7)? (STDERR) throw new Error("Meteor code must always run within a Fiber. " +
W20160602-18:45:01.931(-7)? (STDERR) ^
W20160602-18:45:01.935(-7)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20160602-18:45:01.935(-7)? (STDERR) at Object.Meteor.nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20160602-18:45:01.936(-7)? (STDERR) at [object Object].
.extend.get (packages/meteor/dynamics_nodejs.js:21:1)
W20160602-18:45:01.936(-7)? (STDERR) at Object.Meteor.isRestricted (packages/dispatch_run-as-user/packages/dispatch_run-as-user.js:137:1)
W20160602-18:45:01.936(-7)? (STDERR) at [object Object].Mongo.Collection.(anonymous function) as update
W20160602-18:45:01.936(-7)? (STDERR) at Object.UserPresence.removeConnectionsByInstanceId (packages/konecty_user-presence/server/server.js:62:1)
W20160602-18:45:01.936(-7)? (STDERR) at process. (packages/konecty_user-presence/server/server.js:201:1)
W20160602-18:45:01.937(-7)? (STDERR) at process.emit (events.js:117:20)
W20160602-18:45:01.937(-7)? (STDERR) at process.exit (node.js:750:17)
W20160602-18:45:01.937(-7)? (STDERR) at exitIfDone (packages/dispatch:mocha-phantomjs/server.js:58:17)
W20160602-18:45:01.937(-7)? (STDERR) at ChildProcess.done (packages/dispatch:mocha-phantomjs/server.js:91:7)

Looks like it can be solved by add Meteor.bindEnvironment as suggested to the process.on('exit') handler in server/server.js. Here is the diff from my local package:
diff --git a/server/server.js b/server/server.js
index f2ab324..95a903c 100644
--- a/server/server.js
+++ b/server/server.js
@@ -196,13 +196,13 @@ UserPresence = {
});
});

  •           process.on('exit', function() {
    
  •           process.on('exit', Meteor.bindEnvironment(function() {
                    if (Package['konecty:multiple-instances-status']) {
                            UserPresence.removeConnectionsByInstanceId(InstanceStatus.id());
                    } else {
                            UserPresence.removeAllConnections();
                    }
    
  •           });
    
  •           }));
    
            if (Package['accounts-base']) {
                    Accounts.onLogin(function(login) {
    

Feature:Mobile status to use on ReactNative

Hi, I want to implement the use of a mobile state, I have tried to do it by myself but it does not work as expected.

The expected behavior would be that by setting it as 'mobile' from a method call, it behaves as the 'online' state does.

version: 2.0.1
used on [email protected] and ReactNative API 32

DeepinScreenshot_Seleccionar área_20190603161051

DeepinScreenshot_Seleccionar área_20190603161112

*note: I have fixed the obvious error of the name 'movile'; but he does not behave as he expected.

Unable to add Package

If I try to add this package to my project I get this error:

C:\Users\zcompton\AppData\Local\.meteor\packages\meteor-tool\1.8.1\mt-os.windows.x86_64\dev_bundle\lib\node_modules\meteor-promise\promise_server.js:218
      throw error;
      ^

Error: ENOENT: no such file or directory, open 'C:\Users\zcompton\AppData\Local\Temp\mt-1to2b26.8ptjj\os\konecty:user-presence-tests.js'
    at Object.fs.openSync (fs.js:646:18)
    at Object.wrapper [as open] (C:\tools\fs\files.js:1641:35)
    at Object.files.readBufferWithLengthAndOffset (C:\tools\fs\files.js:1887:20)
    at C:\tools\isobuild\unibuild.js:122:26
    at Array.forEach (<anonymous>)
    at Function._.each._.forEach (C:\Users\zcompton\AppData\Local\.meteor\packages\meteor-tool\1.8.1\mt-os.windows.x86_64\dev_bundle\lib\node_modules\underscore\underscore.js:79:11)
    at Function.fromJSON (C:\tools\isobuild\unibuild.js:119:7)
    at C:\tools\isobuild\isopack.js:921:33
    at Array.forEach (<anonymous>)
    at Function._.each._.forEach (C:\Users\zcompton\AppData\Local\.meteor\packages\meteor-tool\1.8.1\mt-os.windows.x86_64\dev_bundle\lib\node_modules\underscore\underscore.js:79:11)
    at Isopack._loadUnibuildsFromPath (C:\tools\isobuild\isopack.js:908:7)
    at _.each (C:\tools\packaging\tropohouse.js:521:21)
    at Array.forEach (<anonymous>)
    at Function._.each._.forEach (C:\Users\zcompton\AppData\Local\.meteor\packages\meteor-tool\1.8.1\mt-os.windows.x86_64\dev_bundle\lib\node_modules\underscore\underscore.js:79:11)
    at buildmessage.enterJob (C:\tools\packaging\tropohouse.js:520:13)
    at Object.enterJob (C:\tools\utils\buildmessage.js:388:12)
    at C:\tools\packaging\tropohouse.js:515:22
    at Object.enterJob (C:\tools\utils\buildmessage.js:388:12)
    at Object.download (C:\tools\packaging\tropohouse.js:427:20)
    at C:\tools\packaging\tropohouse.js:600:22
    at Object.enterJob (C:\tools\utils\buildmessage.js:388:12)
    at exports.Tropohouse.downloadPackagesMissingFromMap (C:\tools\packaging\tropohouse.js:597:20)
    at C:\tools\project-context.js:836:25
    at Object.enterJob (C:\tools\utils\buildmessage.js:388:12)
    at C:\tools\project-context.js:835:20
    at C:\tools\packaging\catalog\catalog.js:100:5
    at Object.capture (C:\tools\utils\buildmessage.js:283:5)
    at Object.catalog.runAndRetryWithRefreshIfHelpful (C:\tools\packaging\catalog\catalog.js:99:31)
    at ProjectContext._downloadMissingPackages (C:\tools\project-context.js:834:13)
    at C:\tools\project-context.js:300:9
    at Object.enterJob (C:\tools\utils\buildmessage.js:388:12)
    at ProjectContext._completeStagesThrough (C:\tools\project-context.js:290:18)
    at Profile.run (C:\tools\project-context.js:282:12)
    at Function.run (C:\tools\tool-env\profile.js:490:12)
    at ProjectContext.prepareProjectForBuild (C:\tools\project-context.js:281:13)
    at C:\tools\runners\run-app.js:547:29
    at Object.capture (C:\tools\utils\buildmessage.js:283:5)
    at bundleApp (C:\tools\runners\run-app.js:546:31)
    at AppRunner._runOnce (C:\tools\runners\run-app.js:613:35)
    at AppRunner._fiber (C:\tools\runners\run-app.js:922:28)
    at C:\tools\runners\run-app.js:401:12

Any idea on how to resolve this issue? I am able to get it to work if I force version 2.2.0 like this: konecty:user-presence@=2.2.0.

2.3.0 and 2.4.0 give me the above error.

Callbacks?

Is there a callback I can call when a user goes online/offline?

In my case, I want to set up an eventlistener when a user gets online, and detach it again when they go offline

UserPresence.activeLogs and callback method are not matching

Hi all,

I am using Rocket.chat for my project and need to keep track if visitor is online, away or offline.

My Code:

Meteor.startup(() => {
	UserPresenceEvents.on('setStatus', (session, status, metadata) => {
		console.log('setStatus - status:  ', status);
		console.log('setStatus - session: ', session);
		console.log('setStatus - metadata: ', metadata);
		const logs = UserPresence.activeLogs();
		console.log('UserPresence - log: ', logs);

		if (metadata && metadata.visitor) {
			RocketChat.models.LivechatInquiry.updateVisitorStatus(metadata.visitor, status);
			RocketChat.models.Rooms.updateVisitorStatus(metadata.visitor, status);
		}
	});
});

I leave the client for 60 seconds so a message away is sent from client to server. The server part of UserPresence captures that correctly, but somehow my callback method receives a different value

Server's log:

I20190804-16:18:33.801(10)? [user-presence] setConnection tb5TEakSDWMqPWPG5 fKcSmu6Xs3zxucra5 away
I20190804-16:18:33.831(10)? setStatus - status:   online
I20190804-16:18:33.831(10)? setStatus - session:  tb5TEakSDWMqPWPG5
I20190804-16:18:33.832(10)? setStatus - metadata:  { visitor: 'tb5TEakSDWMqPWPG5' }

When I turned off the tab, the server receives a remove connection message, but my method callback still receives an online status. I am doing this correctly, or any setting needs to be turned on?

I20190804-16:30:48.574(10)? [user-presence] removeConnection fKcSmu6Xs3zxucra5
I20190804-16:30:48.586(10)? setStatus - status:   online
I20190804-16:30:48.586(10)? setStatus - session:  2DBBuWjyvEbf2maLN
I20190804-16:30:48.587(10)? setStatus - metadata:  undefined

Using the app with two apps and one database causes problems

I've just deployed two apps that share a mongodb. When I uploaded the second app, all the users that were online in the first app were set to offline.
Not sure why that happened. Will have to dig into the code to understand, but it seems wrong if they're sharing the same database.

Being able to attach some state to a user presence

Other similar packages like {tmeasday or 3stack}:presence allow the developer to attach some state relate to the user presence. One use case of this is to be able to tell where the user currently is — for instance to report if the user is present in the current chatroom.

Is this a feature you would consider?

I ask because I try to switch Wekan to use this package (because 3stack:presence doesn’t work well with fast-render, see https://github.com/3stack-software/meteor-presence/pull/3) and I currently have something like that to indicate if the user is present in the the current board:

Presence.configure({
  state() {
    return {
      currentBoardId: Session.get('currentBoard'),
    };
  },
});

Is it compatible with ObjectiveDDP

I'm using objectiveDDP in my iOS app, is this package compatible with any ddp connection or it requieres client side Javascript to work?

Error: Did not check() all arguments during call to 'UserPresence:online'

Hi, I tried to use this package (konecty:[email protected]) in a Meteor 1.7.1-rc.5 project and get this error when running and visiting the app:
Error: Did not check() all arguments during call to 'UserPresence:online'

client:

Meteor.startup(() => {
  // Time of inactivity to set user as away automaticly. Default 60000
  UserPresence.awayTime = 180000; // 3 minutes
  // Start monitor for user activity
  UserPresence.start();
});

server:

Meteor.startup(() => {
  // Listen for new connections, login, logoff and application exit to manage user status and register methods to be used by client to set user status and default status
  UserPresence.start();
  // Active logs for every changes
  // Listen for changes in UserSessions and Users to set user status based on active connections
  UserPresenceMonitor.start();
});

[Feature] Show non-online time on users

(copied from the same issue on Rocket.Chat)
(This is something I'm about to do a PR on and have started the coding already)
Given that I am a user
I would like on Direct Messages and the Member list to see how long a user has been in a non-online status e.g.
(away icon) User A 15 minutes
(offline icon) User B 2 days
(online icon) User C

This is a critical feature for some corporate users for adoption of Rocket.Chat
Note this also will require a PR to Konnecty/meteor-user-presence

Meteor Method to Get a User's Status

This would be helpful for 3rd party applications which use your application's ddp api and also provide an easy way to wrap a rest api around for getting the status of a user.

Crash using fast-render

I'm getting an issue when using user-presence with fast-render. The issue is related to publications, not sure how to fix it in user presence.

I created an issue on fast-render (kadirahq/fast-render#130 (comment)) but found it's related to user-presence.

Any idea on how to fix it?

It's possible to have a user online even if he isn't

I got into a situation when running the app locally, that even when I closed all my web browsers and restarted Meteor that a user was still listed as online.
The UserSessions collection also had a session in it.

this.ready() not called in publication

This code doesn't call this.ready().

        Meteor.publish(null, function() {
            if (this.userId == null && this.connection.UserPresenceUserId != undefined) {
                UserPresence.removeConnection(this.connection.id);
                delete this.connection.UserPresenceUserId;
            }
        });

Remove special chars on file names

Windows filename cannot contain the following symbols
/:*?"<>|

Error: ENOENT: no such file or directory, open 'C:\Users\Administrator\AppData\Local\Temp\mt-1sr25xm.6u99\os\konecty:user-presence-tests.js'

Make me debug along while!

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.