NodeJS Delayed Socket.IO

I am using an example from the Socket.IO home page (http://socket.io/). It works and everything, but there is a huge delay between sending time data and receiving data on the other end.

I am using XAMPP, I have socket.html in my directory and navigate to it using "http: //localhost/socket.html" in my browser and I have a 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> 
+4
source share
3 answers

I found a problem.

On 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. He will try to use them in that order. The rememberTransport function makes the server and client forget which connection they last used and try to connect to the "transport" above.

On the client side, I just did the same. I added:

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

to the socket constructor. It looked like this:

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

Now it works fine.

Thanks guys.

+6
source

Have you tried with a longer message?

There is always a buffer in the socket. If you send less than X bytes, there may be some time to wait until the buffer is flushed, because it was not full.

+1
source

Which browser are you using? Socket.IO degrades to polling, which will be much slower than the browser’s built-in web sockets or flash polling.

0
source

Source: https://habr.com/ru/post/1384238/


All Articles