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>
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.
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 thetransports
array. On recent versions ofsocket.io
andengine.io
the correct arguments and implementation looks like this:And on the client side:
What browser are you using? Socket.IO degrades down to polling, which would be much slower than native browser web sockets or flash polling.
I have found the problem.
In the server I changed:
to
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:
to the socket constructor. So it looked like:
Now it seems to work perfectly.
Thanks guys.