Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to avoid Nagle algorithm effects using webSockets?

I am using webSockets to connect a javascript webSocket client to a java webSocketServer (from an Android application), using the Java-WebSocket library. The Android app sends a small message every few milliseconds to the javascript client.

Using the basic (and intuitive) approach for this situation, the delay between received messages, measured inside the javascript Client show (aproximately) the following pattern: 200 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 200 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 200 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms, 0.1 ms ...

This is the effect of the Nagle algorithm, that is set by default, clumping several messages before sending them.

Since I have not found a way to guarantee its deactivation, I follow the approach proposed in this old question, sending an acknowledge message from the client to the server, and the system behaves properly, but since the acknowledge message has no real purpose (it is more a hack), it should be avoided.

The question is, this keeps being the best solution to this problem? Do you know any way to avoid the clumping?

Thank you.

like image 530
Alex Avatar asked Oct 15 '14 12:10

Alex


People also ask

How do I turn off Nagle algorithm?

To disable Nagle's buffering algorithm, use the TCP_NODELAY socket option. To disable Delayed ACKs, use the TCP_QUICKACK socket option. Enabling the TCP_NODELAY option turns Nagle's algorithm off.

How Nagle's algorithm improves the efficiency of TCP?

Nagle's algorithm is a TCP optimization that makes the stack wait until all data is acknowledged on a connection before sending more data. This process, called "nagling", increases the efficiency of a network application system by decreasing the number of packets that must be sent.

How do I know if Nagle is disabled?

The most direct way would be to trace the setsockopt system call. Looking from the outside you can only notice when it disables Nagle and behaves bad (sends lots of small fragments rapidly). If it disables Nagle and behaves well, you cannot notice from outside.


1 Answers

Since no flush or setTCPNoDelay mechanisms seem to be present on the used library, and no other solutions have been proposed, it seems that the acknowledge message solution remains valid as the best solution to this problem.

like image 86
Alex Avatar answered Oct 05 '22 14:10

Alex