Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS with Socket.IO delay emitting data

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>
like image 721
trentr Avatar asked Dec 02 '11 01:12

trentr


People also ask

Does Socket.IO use long polling?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

Is Socket.IO emit asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.

Is Socket.IO real time?

Socket.io enables real-time communication between a server and a client. It is build around the websocket protocol, and provides additional features such as client specific data and multi-socket broadcasting.

How much traffic can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).


3 Answers

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.

like image 152
trentr Avatar answered Oct 04 '22 22:10

trentr


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.

like image 43
msimr-aptgeek Avatar answered Oct 04 '22 22:10

msimr-aptgeek


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'] });
like image 35
Gary Weiss Avatar answered Oct 04 '22 23:10

Gary Weiss