Last Updated: February 25, 2016
·
1.934K
· benjaminrh

Closed Window and Lost Focus Event Hooks with Meteor

If you're building an app with Meteor, maybe a game or something, you might have wanted to react on the server when a user switches to a different window (loses focus), switches back, or exits altogether. I've just finished the first version of an easy-to-use package that does just that!

It's called meteor-event-hooks, and to start using it, just run mrt add event-hooks. Then stick Hooks.init() in your Meteor.startup, and you're good to go. But what can you do?

Currently, you can provide callbacks for Hooks.onLoseFocus and Hooks.onGainFocus both on the client and on the server, and you can tune how often you want to check by setting Hooks.updateFocus. Maybe you're making a game engine and need to pause instantly, or maybe it's a social platform and a few seconds is better. You also have access to Hooks.onCloseSession on the server-side only, which runs when a user exits your app by closing the tab/window.

I made this for my own apps, but if you have suggestions I'd love to hear them! Also, I'll be adding more events as the need arises.