How to catch when a user leaves a page in Meteor and / or Iron router?

I try to catch when a user leaves Meteor from my application (version 1.2.0.2); something equivalent to SocketIO disconnect() on the server side.

The user can close the browser, go to another site or just refresh the page , and he will still run

Surprisingly, I search the Internet and everything is messed up, nothing works. I thought the Meteor was literally based on this magic in real time, so it needs to manage this event anyway.

Iron router , indicate this:

onStop: called when the route is stopped, usually right before the new route is launched.

I also found Router.load and Router.unload , but none of them work. This is my current [not working] code, which is pretty simple

 Router.configure layoutTemplate: 'MasterLayout' loadingTemplate: 'Loading' notFoundTemplate: 'NotFound' Router.onStop (-> console.log('Try to stop') Users.insert({ name: "This is a test" lat: 0 lng: 0 }) ) 

Am I doing something wrong here? How do you catch this event in your application?

+6
source share
2 answers

There were two solutions, and I found the 2nd and the best by doing some time in the API documentation .

First decision: work with subscription and publication

Anywhere on the controller side / front-end you must subscribe to the collection

 # in coffee @subscribe('allTargets') # in javascript this.subscribe('allTargets') 

After that, you just need to publish and add the onStop listener. In this example, the Targets collection that I already defined somewhere earlier just gets all the records.

 # in coffee Meteor.publish 'allTargets', -> @onStop -> # Do your stuff here return Targets.find() # in javascript Meteor.publish('allTargets', function() { this.onStop(function() { // Do your stuff here }); return Targets.find(); }); 

You must be careful not to return Targets.find() before you set the onStop . I do not think this is the ideal solution, since you are not listening to the connection itself, but to the collection changes.

Second solution: working with a DDP connection

I understood through the Meteor API Documentation we can directly listen to the connection and see if someone is disconnecting from the server side.

To stay well organized and clean as part of the Meteor Iron project, I added a new file to app/server/connection.coffee and wrote this code

 # in coffee Meteor.onConnection (connection) -> connection.onClose -> # Do your stuff # in javascript Meteor.onConnection(function(connection) { connection.onClose(function() { // Do your stuff }); }); 

You can manage data using connection.id , which is the unique identifier for your browser tab. Both solutions work well for me.

If you use Meteor.userId through your account system, you cannot use it outside the method on the server side, so I had to find a workaround with connection.id .

If someone has the best solution for managing connections when receiving this kind of client data, feel free to give your data.

+3
source

You need to connect to the onStop route, not the router. For instance:

 Router.route('/', { onStop: function() { console.log("someone left the '/' route"); } }); 

Another option is to use the onStop event of the subscription . This is probably the option most similar to the disconnector you mentioned. You can find an example of this in the phone source code .

+3
source

All Articles