I've set up a node express server to power an instant chat feature on my website. One issue I'm facing is that when a user closes their browser, their status doesn't change to 'offline' on their friend's buddy list. I know there's a function in node called req.on('close', function(){}); but I'm unsure how to integrate it into my server file. Any tips or guidance would be highly appreciated. Below is a snippet of relevant code from my express server:
var app = express();
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(require('./middleware/im')({
maxAge: 60 * 1000,
reapInterval: 60 * 1000,
authentication: require('./libs/authentication/' + AUTH_LIBRARY)
}));
app.set('root', __dirname);
// Listener endpoint; handled in middleware app.get('/listen', function(){});
app.post('/message', function(req, res) {
res.find(req.body['to'], function(user) {
if(!user)
return res.send(new packages.Error('not online'));
res.message(user, new packages.Message(
req.session.data('username'),
req.body.body
));
});
});
});
If you come across any issues while using the above code, feel free to seek further advice from the community.