diff --git a/README.md b/README.md index 334f3cf..e7c6624 100644 --- a/README.md +++ b/README.md @@ -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}}); }); ``` diff --git a/presence_client.js b/presence_client.js index bc36a9d..7aaace5 100644 --- a/presence_client.js +++ b/presence_client.js @@ -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 @@ -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) { @@ -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; diff --git a/presence_server.js b/presence_server.js index a67617e..198d8c3 100644 --- a/presence_server.js +++ b/presence_server.js @@ -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() };