Question

I am using the example from the Socket.IO homepage (http://socket.io/). It works and everything, but there is a huge delay between the time data is sent, and when that data is received on the other end.

I am using XAMPP, I have socket.html in my dir, and navigate to it using "http://localhost/socket.html" in my browser, and I have the server listening on port 8080.

Server:

var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
 socket.emit('news', { hello: 'world' });
 socket.on('my other event', function (data) {
   console.log(data);
 });
});

HTML File:

<html>
<head>
    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    </script>
</head>

<body>

</body>
</html>
Was it helpful?

Solution

I have found the problem.

In the server I changed:

var io = require('socket.io').listen(8080);

to

var io = require('socket.io', { rememberTransport: false, transports: ['WebSocket', 'Flash Socket', 'AJAX long-polling'] }).listen(8080);

which forces the server to use either WebSockets, Flash Sockets, or long-polling. It wil try to use those in that order. The rememberTransport forces the server and client to forget which connection it used last, and try to connect with the 'transports' above.

On the client side I just pretty much did the same thing. I added:

{ rememberTransport: false, transports: ['WebSocket', 'Flash Socket', 'AJAX long-polling']}

to the socket constructor. So it looked like:

var socket = io.connect('http://localhost:843', { rememberTransport: false, transports: ['WebSocket', 'Flash Socket', 'AJAX long-polling']});

Now it seems to work perfectly.

Thanks guys.

OTHER TIPS

Have you tried with a longer message?

There is surely a buffer on the socket. If you send less than X bytes there might be some wait time before the buffer is flushed since it was not filled.

What browser are you using? Socket.IO degrades down to polling, which would be much slower than native browser web sockets or flash polling.

Using websockets improves the behavior in the sense that it disables buffering. Websockets is implemented with setNoDelay(true) as can be seen in the websockets code so it will not buffer messages.

You can request websockets explicitly by placing the word websocket first inside the transports array. On recent versions of socket.io and engine.io the correct arguments and implementation looks like this:

import socketIO from 'socket.io';
const server = express();
const requestHandler = server.listen(PORT, () => console.log(`Listening on ${PORT}`));
const io = socketIO(requestHandler, { transports: ['websocket', 'polling'] });

And on the client side:

import io from 'socket.io-client';
let socket = io(SERVER_URL, { transports: ['websocket', 'polling'] });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top