I am giving a try to Node.js with socket.io
Now here is my scenario i am ubuntu 12.04 user and i have folder pp on desktop
inside that i am putted server file i.e app.js
Here is the content
var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
console.log('Listening at: http://localhost:8080');
});
socketio.listen(server).on('connection', function (socket) {
socket.on('message', function (msg) {
console.log('Message Received: ', msg);
socket.broadcast.emit('message', msg);
});
});
Now in the same folder i have another file index.html like
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(function(){
var iosocket = io.connect();
iosocket.on('connect', function () {
$('#incomingChatMessages').append($('<li>Connected</li>'));
iosocket.on('message', function(message) {
$('#incomingChatMessages').append($('<li></li>').text(message));
});
iosocket.on('disconnect', function() {
$('#incomingChatMessages').append('<li>Disconnected</li>');
});
});
$('#outgoingChatMessage').keypress(function(event) {
if(event.which == 13) {
event.preventDefault();
iosocket.send($('#outgoingChatMessage').val());
$('#incomingChatMessages').append($('<li></li>').text($('#outgoingChatMessage').val()));
$('#outgoingChatMessage').val('');
}
});
});
</script>
</head>
<body>
Incoming Chat: <ul id="incomingChatMessages"></ul>
<input type="text" id="outgoingChatMessage">
</body>
when i am trying to run the app.js useing node like
node app.js
I am getting the error
warn - error raised: Error: listen EADDRINUSE
I go through some doc and found that port are busy so restarted the system but still i am getting the same error .
Please tell me what might i am doing wrong .
Try another port. 8080 is usually used by tomcat e.g.
Use netstat -a -v
to know which port are currently used.
It looks to me that your not connecting to the host on the client side:
var iosocket = io.connect('http://localhost:8080');
You are not connecting to your server, this is wrong:
var iosocket = io.connect();
this is right:
var iosocket = io.connect('http://localhost:8080');
Also your port 8080 is used on your server by "http-proxy", try an other port.
An ultra-silly/simple possibility is to make sure you don't have multiple terminal windows open. I got this error and realized I had a second pre-existing connection that I had started up my app with. When I closed it and restarted the app the error went away.