28

Am a beginner with websockets. Have a need in my application where server needs to notify clients when something changes and am planning to use websockets.

  1. Single server instance and single client ==> How many websockets will be created and how many connections to websockets?

  2. Single server instance and 10 clients ==> How many websockets will be created and how many connections to websockets?

  3. Single server instance and 1000 clients ==> How many websockets will be created and how many connections to websockets?

How do you scale with webscokets when your application has a 1000’s of user base?

Thanks much for your feedback.

user157251
  • 64,489
  • 38
  • 208
  • 350
user3216514
  • 465
  • 1
  • 8
  • 11
  • Possible duplicate of [Do HTML WebSockets maintain an open connection for each client? Does this scale?](https://stackoverflow.com/questions/4852702/do-html-websockets-maintain-an-open-connection-for-each-client-does-this-scale) – Scott Stensland Nov 13 '17 at 15:44
  • best way to find out is to just do it ... create your server then write new code to spin up tons of clients ... many tools make this easy to do ... websockets has been designed to handle many 10's if not 100's of thousands of simultaneous client connections – Scott Stensland Nov 13 '17 at 15:45

1 Answers1

52

1) Single server instance and single client ==> How many websockets will be created and how many connections to websockets?

If your client creates one webSocket connection, then that's what there will be one webSocket connection on the client and one on the server. It's the client that creates webSocket connections to the server so it is the client that determines how many there will be. If it creates 3, then there will be 3. If it creates 1, then there will be 1. Usually, the client would just create 1.

2) Single server instance and 10 clients ==> How many websockets will be created and how many connections to websockets?

As described above, it depends upon what the client does. If each client creates 1 webSocket connection and there are 10 clients connected to the server, then the server will see a total of 10 webSocket connections.

3) Single server instance and 1000 clients ==> How many websockets will be created and how many connections to websockets?

Same as point #2.

How do you scale with webscokets when your application has a 1000’s of user base?

A single server, configured appropriately can handle hundreds of thousands of simultaneous webSocket connections that are mostly idle since an idle webSocket uses pretty much no server CPU. For even larger scale deployments, one can cluster the server (run multiple server processes) and use sticky load balancing to spread the load.

There are many other articles like these on Google worth reading if you're pursuing large scale webSocket or socket.io deployments:

The Road to 2 Million Websocket Connections in Phoenix

600k concurrent websocket connections on AWS using Node.js

10 million concurrent webSockets

Ultimately, the achievable scale per a properly configured server will likely have more to do with how much activity there is per connection and how much computation is needed to deliver that.

jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • Thanks much for your time and the feedback! Another quick question..."A single server, configured appropriately can handle hundreds of thousands of simultaneous webSocket connections that are mostly idle since an idle webSocket uses pretty much no server CPU." is this using in-memory broker or external broker relay? – user3216514 Nov 13 '17 at 16:54
  • @user3216514 - I don't even know what you mean by an in-memory or external broker relay. Perhaps you should define better what you're asking about and post a new question on that topic. – jfriend00 Nov 13 '17 at 17:09
  • 1
    How do you handle clustering with websockets? – user3216514 Nov 30 '17 at 18:10
  • @user3216514 - That's not something we can answer here in a comment (it's a much longer answer). If you do a search and don't find what you're looking for (though I know for a fact, there are a number of questions/answers on that topic here), then you should post your own question on clustering. – jfriend00 Nov 30 '17 at 22:24
  • When a website is accessed, then, on the server, does it open a web socket? 1 web socket per connection? – variable Jun 09 '20 at 07:19
  • @variable - Websockets are opened by client-side Javascript code in your web page. So, it depends entirely upon what the client-side Javascript you put in your web pages does. If it opens one webSocket per open web page, then that is what you would have. If it only opens the webSocket under certain circumstances on certain pages, then you'd have fewer of them. – jfriend00 Jun 09 '20 at 13:38
  • What happens at the server level. Say there are 100 different requests for a web page at port 80. Then how does the server handle concurrent requests? I though this was done using web sockets? – variable Jun 10 '20 at 06:17
  • @variable - Concurrent http requests for a web page doesn't have anything to do with webSockets. A webSocket is a different type of connection for a different purpose. – jfriend00 Jun 10 '20 at 07:25