latency issue with socket.io(websockets)

2019-06-03 05:17发布

we are planning to create a real time SPA using node.js and we are testing for the latency, please go through the following link :

http://173.200.239.98:6060/

there are 2 area...when user hover the mouse over a textarea in right side, we are printing the latency in the left side textarea and the latency is in milliseconds. the ques is that latency varies from 0.3 secs to 6 secs..is it normal with web sockets? or am i doing something wrong?

NOTE:- the server is in detroit,usa and i am accessing the server from india chennai.

SOURCE CODE:

    <!DOCTYPE html>
     <html>
  <head>
    <meta charset='utf-8'/>
  </head>
  <body style='margin:0px' >

    <table>
    <tr>
        <th>latency in milliseconds</th>
        <th>hover to trigger message</th>
    </tr>
    <tr>
        <td><textarea id='message' name='message' rows="20" cols="20" ></textarea></td>
        <td><textarea id='hover' name='hover' rows="20" cols="20" ></textarea></td>
    </tr>
    </table>

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        jQuery(function($){

            var socket = io.connect();
            var messagearea=document.getElementById("message");
            var workarea=document.getElementById("hover");
            workarea.onmousemove=function(){


                var startTime = Date.now();
                socket.emit('ping',startTime);

            };

            socket.on('pong', function(startTime) {
                var latency = Date.now() - startTime;
                messagearea.innerHTML=  messagearea.innerHTML + latency + '\n';
                messagearea.scrollTop = messagearea.scrollHeight;

            });

        });
    </script>   

  </body>
</html>

3条回答
我只想做你的唯一
2楼-- · 2019-06-03 05:47

There is nothing intrinsic in WebSocket that should cause inconsistent latencies. If that's what you are getting, try doing something else other than WebSocket over the same network route and observe the latencies. For example, ping or FTP.

If you get the same erratic latencies, then you know it's nothing to with the WebSocket connection. If you don't get the same erratic latencies, then it might be something to do with your browser or JavaScript. Perhaps it's slow to process or render the messages?

Basically you need to start peeling away each layer to try and discover which one is adding the latency.

查看更多
叛逆
3楼-- · 2019-06-03 06:00

I get nearly identical results with both a ping test and your app. Since the ping test takes your client/server out of the equation and uses a well known networking tool - if you get similar results in your app as with a ping test, then your client/server are doing pretty well and not a source of the delay.

Now when you run the test from India to Detroit, you're doing networking half way around the world and across at least one major ocean. It's not surprising if there is some latency and not surprising if that latency varies. Just to give you an idea what has to happen is that you send some TCP packets from India to Detroit. They travel through many, many different routers and service providers and cables/fibers to eventually get to the server in detroit. Because it's TCP, confirmation of delivery has to be sent back along the reverse route. Then your server gets the packet and does it's thing sending TCP packets back the other way which again need to be confirmed with return packets.

I ran your app and got these results:

enter image description here

I ran a ping test with ping -n 20 173.200.239.98 from my location in San Jose, CA and got these results:

enter image description here

As you can see, the ping test gave 73-87ms. Your app game 74-83ms. Those are basically identical. It seems like your client and server are doing just fine.

I'd suggest you run your own ping test from your location and see how much of a difference there is between your ping test and your app results.

If you want to see the route that your packets take with some info on transit time to each stop, you can run this command:

tracert 173.200.239.98

And it will show you the time (in ms) to each successive major hop along the way to the destination.

查看更多
手持菜刀,她持情操
4楼-- · 2019-06-03 06:03

Obviously its dependant on the location you are. I think that's how the internet works. Latency is pretty low here in Germany: Between 120 and 160 milliseconds... that's fast...

查看更多
登录 后发表回答