53

HTML5 websockets are (and have been for some time) a hot topic as they elegantly enable real-time server-side push.

I currently have a working application with websockets powered by Tomcat 7.0.30 which includes websocket support. But moving this to a production environment raises questions.

Mainly I would like to know the possible maximum number of connections that can operate (be open) concurrently per browsing session; a browsing session implies a single browser tab or window.

Do open websocket connections add up to the maximum number of connections that can be processed simultaneously by the Web server? E.g. MaxClients in Apache.

Conversely, is the maximum number of websockets for a single browsing session limited by the browser itself? As this blog post shows, up to April 2012, different browsers support varying amounts of open websocket connections. (I personally would aim for 1 open websocket per browsing session; but this info would still be good to know).

TL/DR:

  1. What limits the amount of possible websockets per browsing session? Is it the client? The server? Or a combination of both?
  2. Does the same limitation(s) apply to both ws: and wss: connections?
Community
  • 1
  • 1
Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
  • 1
    I highly recommend keeping a single connection per browsing session - the last thing you want is to worry about cross-browser limitations. Your server is where it is really going to matter. Servers and firewalls can only handle so many concurrent connections. I have seen socket-enabled hosting providers base their pricing on the number of concurrent connections, if that gives you any idea about where it matters and how you should build your app (eg. single connection per user). – Ryan Wheale Feb 09 '14 at 23:53
  • Worth reading. http://drewww.github.io/socket.io-benchmarking/ –  Feb 11 '14 at 07:12

2 Answers2

37

There isn't a standard specification of max-connections default value for browsers.It depends on implementation [0]. Furthermore using more than a web-socket per browsing session for the same application seems overkill since you can use pub/sub channels.

Bottleneck for connections usually is at server side. Web-socket is a upgrade to HTTP so connections are "just" upgraded HTTP(TCP) connections [1].HTTPS and WSS add just a security layer to the normal connection.They are not a different connection [2]. In your case check maxConnections (and maxThreads) [3] and your Operating System maximums [4][5]. If your concurrent connections reach tens of thousands maybe you should start thinking on load balancing or clustering [6].

[0]https://code.google.com/p/chromium/issues/detail?id=85323

[1]http://en.wikipedia.org/wiki/WebSocket

[2]http://en.wikipedia.org/wiki/HTTP_Secure

[3]http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

[4]https://serverfault.com/questions/10852/what-limits-the-maximum-number-of-connections-on-a-linux-server

[5]https://superuser.com/questions/251596/is-there-a-hard-limit-of-65536-open-tcp-connections-per-ip-address-on-linux

[6]http://tomcat.apache.org/tomcat-7.0-doc/config/cluster.html

More about high concurrency: http://www.kegel.com/c10k.html

Community
  • 1
  • 1
Mardie
  • 1,663
  • 17
  • 27
  • 11
    Technically, WebSocket is not an 'upgrade' to HTTP. It is a different protocol that also transmits on top of TCP. It does use HTTP for handshaking and to send an upgrade header field to initiate protocol switching. – hacklikecrack Apr 08 '14 at 08:39
  • @hacklikecrack But, you negotiate it over HTTP, to an HTTP server, it continues to flow through said HTTP server as an HTTP request, and notably is negotiated with `Connection: Upgrade` as an HTTP header. I have to side with @Mardie on this one ;) – trevorj Jun 08 '18 at 02:21
  • 1
    What is the "pub/sub channels" feature you're referring to? – Glyph Jun 07 '19 at 17:39
  • While the *protocols* of HTTP and Websocket differs greatly, Websocket is very much is an upgrade of the actual HTTP connection. The same TCP connection is used, also including much of the HTTP specific context when the UPGRADE command is called. While much of the context can be renegotiated during the handshake, elements that aren't will carry over from the pre-existing HTTP connection. – JSON Jul 31 '22 at 15:51
7

In Gecko 7 they introduced the aprameter network.websocket.max-connections you can set it in about:config. It's setting the maximum websocket connections "at a time" according to this: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

I don't know if you can determine this number from the code and if there is any way to determin how many is open in other sessions (so how many you have left).

OlivierBlanvillain
  • 7,701
  • 4
  • 32
  • 51
Mat
  • 2,378
  • 3
  • 26
  • 35
  • Hmm there doesn't seem to be a cross-browser way to set this. Still helpful; +1ed. – Joseph Victor Zammit Sep 18 '12 at 11:05
  • couldn't find a similar option in Chrome, but this was helpful for Firefox, thanks! – Brade Dec 17 '14 at 15:25
  • According to the first reference in the top-voted answer by Mardie, Chrome does not provide such a setting in run time, @Brade. You would have to compile your own custom Chromium to change the limit. – Palec Apr 09 '19 at 06:30
  • My problem was from the user's point of view: when I have too many open tabs with TFS web interface loaded, they all start behaving strangely. Setting larger `network.http.max-persistent-connections-per-server` in FF's about:config helped. – Palec Apr 09 '19 at 06:44