4

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
Jules
  • 53
  • 1
  • 6
  • I've noticed the same thing today on my application. It runs correctly directly on port 16667, but exhibits this problem when running through a proxy setup that redirects port 80 traffic from a domain there. – Kos Mar 07 '13 at 18:46

2 Answers2

2

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);
Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
  • When I have changed it. I still got the problem. The sockets works but takes 10 seconds to get connected (see update above) – Jules Aug 20 '12 at 16:13
  • Weird. Does it behave the same way if you connect to it on localhost? – Linus Thiel Aug 20 '12 at 16:33
  • It's on a external server. (ubuntu server) When setting it to localhost it doesn't do anything. – Jules Aug 21 '12 at 07:53
  • Well... Can you recreate the problem while running it on your own machine? Or running without any potential proxies/firewalls inbetween. – Linus Thiel Aug 21 '12 at 08:48
  • Due the time I will set it up later on my local machine. The firefox console gives me an error as well: Tijdstempel: 21-8-2012 14:18:54 Fout: Firefox kan geen verbinding maken met de server op ws://domain.nl:9250/socket.io/1/websocket/180625581996179685. Bronbestand: http://domain.nl:9250/socket.io/socket.io.js Regel: 2293 Tijdstempel: 21-8-2012 14:19:04 Fout: syntaxisfout Bronbestand: http://domain.nl:9250/socket.io/1/xhr-polling/180625581996179685?t=1345551544221 Regel: 1, Kolom: 1 Broncode: 1 But it does connect... – Jules Aug 21 '12 at 12:21
  • Bump - I'm having a similar issue. – Jacksonkr Dec 18 '13 at 16:37
0

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.

Kos
  • 70,399
  • 25
  • 169
  • 233