I'm trying to get how websokects works on server side.
As i'm get from answers here mostly it next case:
- Browser connects to server
- Server make duplex persistent connection with user
- Server runs instance of code for this user
- This all repeats for other users
So questions is:
Can server handle all users in 1 process? without separating them, so some like global process.
Thank you.
This is highly server dependant.
Some example server implementations / techniques: (obviously not the complete list)
- One process per websocket connection
- One thread per websocket connection
- One thread managing many n websocket connections
- Shared thread pool for all websocket connections.
(Threads pulled in and used on an as-needed basis.
If the connection is idle, the thread is returned to the pool.)
What you'll see is trade-offs with each implementation.
Since websocket is not a request -> response approach, it will be hard to say one approach is better than another. You want to understand how you plan on using websockets to know which approach is the best.
Some thoughts on the subject:
(highly opinion based)
- Is your server side going to be doing heavy processing? (choose technique 1, 2, or 3)
- Is your server side going to be chatty? (choose 3, or 4)
- Are you going to have 1 central websocket server instance, talking to many clients (like a chat server)? (choose 3 or 4)
- Or are you going to have interactive updates that are custom for each user? (like a stock market activity feed) (choose 1, 2, or 4)
- Want to support tens (if not hundreds) of thousands of simultaneous connections? (choose 3 or 4)
- What is your biggest constraint? Memory? CPU? Network Bandwidth?