可以在nodeJS中调用Socket.on()

I want to use nodeJS in my PHP web app. I followed the nodejs tutorial and that works fine when I run on localhost:3000 but I want to run on url like this localhost/final/chat/chat_index.html file. So What I did is following code

chat_index.html

<div id="newUser">
  <form id="user">
    <input id="username">
    <input type="submit">
  </form>
</div> 
$(document).ready(function(){
var socket = io.connect('http://localhost:3000/final/chat/chat_index.html',
                        {resource:'https://cdn.socket.io/socket.io-1.2.0.js'});

$('#user').submit(function(){
socket.emit('new user', $('#username').val());
});

}); // document.ready ends here
</script>

index.js This is server side JS file

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);


app.get('/final/chat/chat_index.html', function(req, res){
  res.sendFile(__dirname + '/chat_index.html');
});



io.on('connection', function(socket){
   console.log('connected user');

   socket.on('new user', function(user){
       console.log(user);
   });  
});

http.listen(3000, function(){
 console.log('listening to port');
});

Above chat_index.html page loads which shows the form on it. When I submit some data through this form server side js is not getting the data.

Is something missing in my code or I am doing something wrong in my code. Thanks in advance

Which version of express are you using? I believe in express 4 it should be:

var http = require('http').createServer(app);

On the client side could you also try using:

var socket = io.connect();

then load the resource in as a script tag?

If you wish to run socket on an specific route, you can use room/namespace

http://socket.io/docs/rooms-and-namespaces/#

Example ( server )

var finalChat = io.of("/final/chat");
finalChat.on('connection', function(socket){
  console.log('connected user');

  socket.on('new user', function(user){
    console.log(user);
  });  
});

If you want Independence private chat rooms, you may want to use id base socket rooms