Nodejs simple socket.io example not working

I've the following Server Side Code:

        var express = require('express')
          , http    = require('http')
          , routes  = require('./routes')
          , io      = require('socket.io')
          , factory = require('./serverfactory.js');
          var  app  = express();

          var server = app.listen(3000);
          io = io.listen(server);

          io.sockets.on('connection',function(socket){

            console.log('new user');

            socket.emit('hail','mysimplemsg');
            socket.on('customevent',function(msg){
                console.log(msg);       
            });
          });



        //var app = module.exports = express.createServer();

        // Configuration

        app.configure(function(){
          app.set('views', __dirname + '/views');
          app.set('view engine', 'jade');
          app.use(express.bodyParser());
          app.use(express.methodOverride());
          app.use(app.router);
          app.use(express.static(__dirname + '/public'));
        });

        app.configure('development', function(){
          app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
        });

        app.configure('production', function(){
          app.use(express.errorHandler());
        });

        // Routes
        app.get('/', routes.index);

And this is the client side :

    var socket = io.connect('http://localhost:3000');
    socket.emit('customevent','work');
    socket.on('hail',function(msg){
        console.log(msg);
    });

I am expecting that my git console outputs new user (which it does) then it should output work (which it does not) then i get a msg in my browser console mysimplemsg (which it does not).

Whats going on why the event at server side that is customevent is not called and why the event at the client side that is hail is not called ?

I believe the issue is that you are emitting customevent from the client before you are connected. Try adding a connect handler and moving your client side emit into it:

var socket = io.connect('http://localhost:3000');    
socket.on('hail',function(msg){
    console.log(msg);
});
socket.on('connect',function(){
                      console.log('connected to socket.io server');
                      socket.emit('customevent','work');
                    });

If the connect handler is not hit then verify that you are referencing the client side socket.io javascript library correctly (jade syntax):

script(type='text/javascript',src='/socket.io/socket.io.js')

Finally figured it out.

It was working fine on opera but not on chrome and firefox. I found this link in which the guy says to insert this code snippet on the server side.

io.configure('development', function(){
  io.set('transports', ['xhr-polling']);
});

It's working fine now.