meteor/meteor — pull request #166 by joshrtay (2012-05-30 17:23:30+00:00)
Title: Fiber middleware and reserved session methods

I found that to keep track of which users are connected, some features needed to be added to Meteor.

## Fiber Middleware
Every time the server starts up a new fiber, the fiber first runs through a middleware stack, passing the middleware the session object and the next function (may want to pass the middleware some other stuff, but this has worked for me so far).

This Fiber middleware is very useful for setting up environment variables.

```javascript
Session.CURRENT_ID = new Meteor.EnvironmentVariable;

Meteor.startup(function() {
Meteor.use(function(session,next) {
Session.CURRENT_ID.withValue(session.id,next);
});
});
```
## Reserved Session Methods
*session* - called when session is created
*connect* - called when client connects
*disconnect* - called when client disconnects
*destroy* - called when session is destroyed

In the users example the reserved methods are used to modify the Sessions collection:

```javascript
Meteor.methods({
session: function() {
Sessions.insert({id: Session.CURRENT_ID.get(), connected: false});
},

connect: function() {
Sessions.update({id: Session.CURRENT_ID.get()}, {$set: {connected: true}});
},

disconnect: function() {
Sessions.update({id: Session.CURRENT_ID.get()}, {$set: {connected: false}});
},

destroy: function() {
Sessions.remove({id: Session.CURRENT_ID.get()});
}
})
```

Check out the simple users app to see these features in action and please let me know what you think.