Is there a limit (practical or otherwise) to the number of web sockets a page opens?

前端 未结 1 1251
余生分开走
余生分开走 2021-02-12 22:49

We really like the async model with web sockets. In some of our applications, we implement widgets in frames (often as many as 10 or 20 widgets on a page). Each widget opens a

1条回答
  •  一生所求
    2021-02-12 23:21

    It depends on the browser.

    See:

    • HTTP simultaneous connections per host limit... are per tab, browser instance or global?
    • HTML5 websockets: max number of open connections?

    It seems that the maximum number of possible open Websockets is defined by the browser implementation, and it is being difficult to find numbers.

    In the Chromium source code (Google Chrome for Linux) I can see a max of 30 per host, 256 overall.

    // Limit of sockets of each socket pool.
    int g_max_sockets_per_pool[] = {
      256,  // NORMAL_SOCKET_POOL
      256   // WEBSOCKET_SOCKET_POOL
    };
    
    // Default to allow up to 6 connections per host. Experiment and tuning may
    // try other values (greater than 0).  Too large may cause many problems, such
    // as home routers blocking the connections!?!?  See http://crbug.com/12066.
    //
    // WebSocket connections are long-lived, and should be treated differently
    // than normal other connections. 6 connections per group sounded too small
    // for such use, thus we use a larger limit which was determined somewhat
    // arbitrarily.
    // TODO(yutak): Look at the usage and determine the right value after
    // WebSocket protocol stack starts to work.
    int g_max_sockets_per_group[] = {
      6,  // NORMAL_SOCKET_POOL
      30  // WEBSOCKET_SOCKET_POOL
    };
    

    In the Firefox configuration, (go to about:config and search for network.websocket) I can see a max of 6 persistent connections per host and 200 overall, but apparently the persistent conection limit does not affect WebSocket connections, so only the 200 limit applies.

    About the approach...

    My recommendation is that you should use one per page/tab. If you have widgets as frames, use .postMessage( +info ) to communicate between frames and the main page, and let the main page communicate with the server with a single connection. Use a publisher/subscriber model to allow different widgets subscribe to particular events.

    Having so many connections is a waste of resources in both browser and server.

    0 讨论(0)
提交回复
热议问题