HTTP streaming server: streams?

I already wrote here about the http chat server I want to create: Alternative http port? This HTTP server must transmit text to each user in the same chat room on the website. The browser will remain connected and wait for further html code. (yes, that works, the browser will not reject the connection).

I had a new question: since this chat server does not need to receive information from the client, there is no need to listen to the client after the server has sent its first response. New chat messages will be sent to the server on a new connection. Therefore, I can open 2 streams, one for waiting for new clients (or new messages) and one for streaming html. Is this a good idea or should I use one thread per client? I donโ€™t think itโ€™s good to have one thread / client when there are many chat users on the Internet, since the server has to handle several different chats with their own rooms.

3 possibilities: 1. One stream for all clients, send text to each client in a row - there should not be much lag, since this is only text it will look like this: user1.send ("text"), user2.send ("text") , ... 2. One thread per chat or chat 3. One thread for the chat user -... many ...

Thanks, I havenโ€™t done much with sockets yet;).

+1
source share
2 answers

I think the simplest template for this simple application is to have a thread pool, and then for each client select an available thread or make it wait until it becomes available.

If you need a solid understanding of the google http server architecture concepts, follow these steps:

  • apache architecture
  • Nginx architecture
0
source

Right now, you seem to be thinking in terms of a given thread that always does the job (type). While this basic design may make sense, it usually doesn't work very well to create such a scalable server.

Often a slightly more abstract point of view works better: you have tasks that you need to perform, and threads that perform these tasks - but the thread doesn't really โ€œcareโ€ about what task it performs.

From this point of view, you just need to create some data structure that describes each task that needs to be performed. When you have a task that you want to do, you fill out the data structure to describe the task and pass it on to complete. Somewhere there are some threads that perform tasks.

In this case, the exact number of threads becomes mostly irrelevant - this is what you can (and do) configure to match the number of processor cores available, the type of tasks, etc., and not what affects the main program design.

+1
source

Source: https://habr.com/ru/post/1303353/


All Articles