title: how to disconnect socket on session expire
body: <p>I have a small express application this also contains the socket program. When ever user success login it creates the session and socket connection perfectly.</p>
  
  <p>But MY problem was when the express session expire or session delete by cookie manager, the socket still in the active connection. it receiving the messages. how to disconnect even if the session are not available after the success login.</p>
  
  <p>Here my code was:</p>
  
  <p>This is my html file which gets the alert message:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script src="jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="socket.io.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  $(document).ready(function(){
  $("button").click(function(){
  $.ajax({
          type: 'GET',
          contentType: 'application/json',
          url: './alert'
      });
  });
  &lt;/script&gt;
  
  &lt;script&gt;
    var socket = io.connect();
    socket.on('message',function(data){
    alert(data.msg);
    });
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;button&gt;alert&lt;/button&gt;
  &lt;/bodY&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my socket and express code:</p>
  
  <pre><code>var express=require('express');
  var app=express();
  var session = require('express-session');
  var server=require('http').createServer(app);
  app.use(session({secret: 'abcd',resave:'false', saveUninitialized:'false',name:'usess',cookie: { maxAge: 10000 }));
  var io=require('socket.io').listen(server);
  
   io.sockets.on('connection', function (socket) {
          socket.join('alerts');
          socket.on('disconnect',function(data){
              socket.leave('alerts');
              console.log('leaved');
          });
      });
  
  app.get('/login', function(req, res){
    //here my authentication code//
    req.session.login='logedin';
    req.session.save();
    res.sendfile('./template.html');
  });
  
  app.get('/', auth,function(req, res){
    //index file contins the two text boxes for user name and pass//
    res.sendfile('./index.html');
  });
  
  app.get('/alert',function(req,res){
    io.sockets.in('alerts').emit('message',{msg:'hai'});
    res.end();
  });
  
  server.listen(3000);
  </code></pre>
  
  <p>Thank You.</p>