Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

websocket

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 web socket to receive notifications of state changes.

Are there any best practices, practical limits, or hard limits on the number of web sockets a page can open?

like image 897
Joe Enzminger Avatar asked Sep 23 '14 20:09

Joe Enzminger


People also ask

Is there a limit to WebSockets?

WebSocket connections have a limit of 5 incoming messages per second. A message is considered: A PING frame. A PONG frame.

How many WebSockets can a browser open?

2) Web browsers allow huge numbers of open WebSockets Instead a far bigger limit holds (255 in Chrome and 200 in Firefox). This blessing is also a curse. It means that end users opening lots of tabs can cause large amounts of load and consume large amounts of continuous server resources.

Can you have more than one WebSocket open?

A server can open WebSocket connections with multiple clients—even multiple connections with the same client. It can then message one, some, or all of these clients. Practically, this means multiple people can connect to our chat app, and we can message some of them at a time.

How long can a WebSocket stay open?

A WebSocket connection can in theory last forever. Assuming the endpoints remain up, one common reason why long-lived TCP connections eventually terminate is inactivity.


1 Answers

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.

like image 179
vtortola Avatar answered Sep 28 '22 00:09

vtortola