I have always used the code below to do this, in another website it worked fine, but now it's not.
server.js
var io = require('socket.io').listen(8080);
var users = {};
console.log("Sever is now running");
io.sockets.on('connection', function (socket) {
console.log("connection");
socket.emit('connected');
});
The server seems to be fine, I think. It shows the console.log message anyways. Though the second console.log doesn't show up, but I just figured it was because of the error or whatever.
data.js
var socket = io.connect(window.location.origin, {port:8080});
$(document).ready(function(){
socket.on('connected', function () {
alert("A");
});
});
On the client code (data.js) add
<script src="/socket.io/socket.io.js"></script> before you set the socket.