Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track browser focus #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Meteor.publish('userPresence', function() {

// ProTip: unless you need it, don't send lastSeen down as it'll make your
// templates constantly re-render (and use bandwidth)
return Meteor.presences.find(filter, {fields: {state: true, userId: true}});
return Meteor.presences.find(filter, {fields: {state: true, focus: true, userId: true}});
});
```

Expand Down
17 changes: 15 additions & 2 deletions presence_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ PRESENCE_INTERVAL = 1000;
// This function will be called a) reactively, b) every 1 second
//
Meteor.Presence = {
// The presnce will contained in, which will be reset to null
// The presence will contained in, which will be reset to null
// when they close the browser tab or log off
state: function() { return 'online'; },

// Track browser focus
focus: true,

// we get told about the sessionId by the server, track it here so we
// overwrite the correct thing
Expand All @@ -23,6 +26,15 @@ Meteor.Presence = {
}
}

// Watch browser focus
Meteor.startup(function() {
window.onfocus = function() {
Meteor.Presence.focus = true;
}
window.onblur = function() {
Meteor.Presence.focus = false;
}
});

// try to maintain sessionId across hot-code-reload
if (Meteor._reload) {
Expand Down Expand Up @@ -58,7 +70,8 @@ Meteor.autorun(function() {

Meteor.call('setPresence',
Meteor.Presence.sessionId,
Meteor.Presence.state(), function(err, sessionId) {
Meteor.Presence.state(),
Meteor.Presence.focus, function(err, sessionId) {
if (err) {
console.log(err);
return;
Expand Down
3 changes: 2 additions & 1 deletion presence_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ PRESENCE_INTERVAL = 1000;

// a method to indicate that the user is still online
Meteor.methods({
setPresence: function(sessionId, state) {
setPresence: function(sessionId, state, focus) {
// console.log(sessionId, state);

// we use the sessionId to tell if this is a new record or not
var props = {
state: state,
focus: focus,
lastSeen: new Date()
};

Expand Down