ApriOri
ApriOri

Reputation: 2688

Check if a user is currently online to meteor server

I'd like to determine if a user is currently "online" or connected to the Meteor server. I need this information before I send the user message, If the user is not connected I'd like to send the message via email.

I know that for traditional web applications that are totally state-less the definition of "online" use is a bit not clear but since modern web frameworks rely on websocket, a user is supposed to be online if a websocket is open.

The question is does Meteor include a method to determine if a user is connected or not?

Upvotes: 1

Views: 1080

Answers (1)

Jankapunkt
Jankapunkt

Reputation: 8423

Summarized: yes, there is such a mechanism.

There are for example package, that store the active login connections of the users with the meteor server and make them available either via an own collection or as part of the user profile.

See: https://github.com/dburles/meteor-presence

(Creates a new collection, called Presences)

or https://github.com/dan335/meteor-user-presence/

(Creates a user's profile entry, called presence. However, has also a collection to store and update the information in the background)

or https://github.com/mizzao/meteor-user-status

(Thanks to blueren in the comments)

Code example (from the first listed package)

Meteor.onConnection(function(connection) {
  // console.log('connectionId: ' + connection.id);
  Presences.insert({ _id: connection.id });
  connections[connection.id] = {};
  tick(connection.id);

  connection.onClose(function() {
    // console.log('connection closed: ' + connection.id);
    expire(connection.id);
  });
});

If you don't want to rely on the packages you may make use of that mechanism yourself.

See: https://docs.meteor.com/api/connections.html#Meteor-onConnection

Upvotes: 2

Related Questions