I have seen some post on this forum regarding Socket.IO and node.js. But still isn't working optimally.
port 80 and port 8080 are used by my webserver for different applications. So in App.js and my socket connection I added port 8081 instead of 8080.
I'm getting the websocket connection invalid message. After 10 seconds or so it has created the websocket and i'm able to use it perfectly well.
But 10 seconds is a too long. Any ideas how to fix this?
script.js:
<script>
var socket = io.connect('http://domainname.nl:8081');
socket.on('connect', function(){
socket.emit('adduser', "<?php echo $user->first_name; ?>");
});
app.js
var app = require('express').createServer()
var io = require('socket.io').listen(8081);
app.listen(8081);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
UPDATE:
When I have changed it. I still got the problem. The sockets works but takes 10 seconds to get connected
info - socket.io started
debug - served static /socket.io.js
debug - client authorized
info - handshake authorized 15619940591959058675
debug - setting request GET /socket.io/1/websocket/15619940591959058675
debug - set heartbeat interval for client 15619940591959058675
warn - websocket connection invalid
info - transport end
debug - set close timeout for client 15619940591959058675
debug - cleared close timeout for client 15619940591959058675
debug - cleared heartbeat interval for client 15619940591959058675
debug - client authorized for
debug - setting request GET /socket.io/1/xhr-polling/15619940591959058675?t=1345479037596
debug - setting poll timeout
debug - clearing poll timeout
You're effectively trying to start two different servers, one for your app
and one for socket.io, both using the same port, 8081
:
var app = require('express').createServer()
var io = require('socket.io').listen(8081);
app.listen(8081);
You can pass a port number to socket.io like this, but if you're going to run it with a node http server you should pass that (i.e., app
) to listen, like so:
var app = require('express').createServer()
var io = require('socket.io').listen(app);
app.listen(8081);
I had the same problem when connecting through a proxy.
My server (express + socket.io, like @Linux G Thiel described) listened at app.example.com:12345
and there was a proxy set up to serve it at example-app.com
(port 80). It worked correctly when accessed through the actual URL, but exhibited the 10-second delay on websocket connection when accessed through the proxy.
I've managed to fix it by simply changing
var socket = io.connect();`
to
var socket = io.connect('http://app.example.com:12345')
in the client-side code.