What is the maximum size of data that can be passed through a WebSocket?

24,187

Solution 1

IT is actually a value very big and probably you don't worry about it.

A single frame, by RFC-6455 base framing, has a maximum size limit of 18,446,744,073,709,551,615 bytes (maximum value of a 64-bit unsigned value).

Try only to make it as little as possible to handle your requirements.

Because the problem is generated on the server side (tomcat). Checking the tomcat documentation I see that:

The default buffer size for binary messages is 8192 bytes. This may be changed for a web application by setting the servlet context initialization parameter org.apache.tomcat.websocket.binaryBufferSize to the desired value in bytes.

So you can change it using updating the org.apache.tomcat.websocket.binaryBufferSize parameter in the configuration file of tomcat.

For additional informations please see the tomcat guide here

Solution 2

About the question related with WebSocket getting disconnected, I had same problem in my Android client that was receiving next message "Binary message size exceeds maximum size".

After looking for some solution in all the Jetty documentation and rest of web pages, the only working option that I have got is to include next code "maxBinaryMessageSize = size * 1024" (where size is your needed maximum limit from client point of view) in the WebSocket class definition, as you can see in next lines:

    import org.eclipse.jetty.websocket.api.BatchMode;
    import org.eclipse.jetty.websocket.api.RemoteEndpoint;
    import org.eclipse.jetty.websocket.api.Session;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
    import org.eclipse.jetty.websocket.api.annotations.WebSocket;

    @WebSocket(maxBinaryMessageSize = 1024 * 1024)
    public class MyClientWebSocket
    {

      @OnWebSocketClose
      public void onClose(int statusCode, String reason)
      {
        closeLatch.countDown();
      }

      @OnWebSocketError
      public void onError(Throwable reason)
      {
        //TODO ACORDARSE DE QUITAR ESTO
        Log.w(TAG, "+++ ERROR : " + reason);
      }

      @OnWebSocketConnect
      public void onConnect(Session session)
      {
        // your code
      }

      @OnWebSocketMessage
      public void onMessageBinary(byte[] message, int offset, int length)
      {
       // message can be processed with a maximum of 1024 * 1024 bytes and not anymore limited to 64 * 1024

      }
    }

If there is some WebSocket expert that can confirm if this is a correct solution from client side (in Java/Android), this would be great. Thanks.

Share:
24,187
theapache64
Author by

theapache64

💻 Creator of Retrosheet ➡️ https://github.com/theapache64/retrosheet 🏗️ Currently building Stackzy ➡️ https://github.com/theapache64/stackzy

Updated on July 09, 2022

Comments

  • theapache64
    theapache64 almost 2 years

    I am new in Java WebSocket. When I tried to pass a JSON with length greater than 8192, the websocket disconnected immediately. How ever JSON with length <= 8191 works fine.

    Is there any MAX SIZE/ LIMIT of data that can be passed through a WebSocket? if yes, what's that size?


    I FIXED THE ERROR BY ADDING THESE LINES TO my web.xml

    <context-param>
             <param-name>org.apache.tomcat.websocket.textBufferSize</param-name>
             <param-value>32768</param-value>
        </context-param>
        <context-param>
                <param-name>org.apache.tomcat.websocket.binaryBufferSize</param-name>
                <param-value>32768</param-value>
    </context-param>
    

    Thanks @Davide Lorenzo MARINO.

  • theapache64
    theapache64 over 8 years
    but why my WebSocket is getting disconnected after sending a data with length > 8191 ? :( .
  • Davide Lorenzo MARINO
    Davide Lorenzo MARINO over 8 years
    It is possible that the server doesn't accept request over 8191 bytes? It is also possible that the problem is on the json parser.
  • theapache64
    theapache64 over 8 years
    am using Tomcat 7 and i can guarantee the problem is not with the JSON parsing. May be the tomcat.
  • Davide Lorenzo MARINO
    Davide Lorenzo MARINO over 8 years
    I edited my answer after you said that the server is a tomcat server
  • theapache64
    theapache64 over 8 years
    Fine. i got it. so it's 8192 bytes.
  • Davide Lorenzo MARINO
    Davide Lorenzo MARINO over 8 years
    If you find this answer useful you can upvote it and flag it as the right response. Thank you and good work
  • Remy Lebeau
    Remy Lebeau almost 3 years
    "A single frame, by RFC-6455 base framing, has a maximum size limit of 18,446,744,073,709,551,615 bytes (maximum value of a 64-bit unsigned value)" - actually, the limit is 9,223,372,036,854,775,807 bytes, because the RFC says "the most significant bit MUST be 0" for a 64bit payload length.