title: fire an event programatically every some seconds using socket.io
body: <p>My server wants to say "hi" to the clients every interval of time. I have this example of code to demonstrate the event firing according to interval of time. 
  But sadly it is not working. </p>
  
  <p><strong>server :</strong></p>
  
  <pre><code>    var app = require('http').createServer(handler),
       io = require('socket.io').listen(app),
       fs = require('fs');
  
  
      app.listen(8001);
  
  
  
       function handler(req, res) {
       fs.readFile(__dirname + '/client1.html', function(err, data) {
        if (err) {
        console.log(err);
        res.writeHead(500);
        return res.end('Error loading client1.html');
      }
      res.writeHead(200);
        res.end(data);
      });
    }
  
    io.sockets.on('connection', function (socket) {
      console.log("runing time");
  
      socket.emit( "clientMessage","hi from server");
      socket.on('clientMessage', function () {
          socket.emit( "serverMessage","hi from server");
      });
      socket.on('disconnect', function () {
          console.log("disconnecting");
      });
    });
  </code></pre>
  
  <p><strong>Client :</strong></p>
  
  <pre><code>   &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
  
     &lt;script&gt;
     var socket = io.connect("http://localhost:8001");
     socket.on('connection', function(socket) {
  
  
         // *this line is not reached*
          alert("connected")
  
        sendTestMessage()
  
      socket.on("serverMessage", function(d) {
          alert("server Message : " + d)
      })
      socket.on('disconnect', function() {
          alert("disconnected")
      });
  
     });
  
  
     function sendTestMessage() {
      alert("test")
      socket.emit('clientMessage', "Test Message From Client");
      setTimeout(sendTestMessage(), 5000)
     }
  
    &lt;/script&gt;
  </code></pre>
  
  <p>Please any help why ?</p>