warmcat / libwebsockets

canonical libwebsockets.org networking library

Home Page:https://libwebsockets.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Questions about better latency in websoket client program

quantking001 opened this issue · comments

Hi friends, I am a newbie about libwebsockets, and I wrote some websocket client programs using the excellent project.

For now, I have some questions about how to get the least latency between my websocket client program and the websocket server. It will be appreciated if you can give some suggestions. :)

I will take a example to better explanation of these questions.

  1. The websocket server path is “wss://wss_example.com/v1”.
  2. After the websocket client establishes the connection with the websocket server, it will send about 100 subscription requests to the server. The requests are like {“channel”: “realtime_update”, “topic”: “A_topic”}, {“channel”: “realtime_info”, “topic”: “B_topic”} and so on.
  3. The websocket server will send updates of topic “A_topic”, “B_topic”, “C_topic”... realtimely to the websocket client. For every topic, the update frequency is about 5ms. So if the client subscribe 100 topics, there will be 100 updates from server to client in every 5ms. The size of every websocket server update is about 200 bytes.
  4. The websocket client parse the server updates which are in json format and saves the result in global buffer.

After I read almost all examples and search the history issues in github about the multiple thread implementation of websocket client, I have some ideas about how to implement the websocket client. The working environment is one single-core cpu and the OS is Linux

  1. One context, one service thread. Make all client subscriptions in one thread, and the websocket server updates are handled in one thread(one event pool).
  2. One context, several service threads. Split the 100 client subscriptions into several threads, and the websocket server updates are handled in different thread(multiple event pool).

First question: Because there is only one single-core cpu, which one is better for me? One service thread or multiple service threads in this scenario?
Second question: Because the websocket server update is small in size, about 200 bytes every update. Will it be more better if I set the pt_serv_buf_size and rx_buffer_size to bigger value?

Or some other suggestions to get the webserver updates as quickliy as possible?

All you reply is appreciated:)

Under these conditions, one service thread. You have one core. He's the only guy who can do anything. If you spread him over n threads that are always active, he's not working on n - 1 threads at any given time while he flits between the threads. So it's meaningless to talk about worker threads in this kind of case with one core.

Your peer will be endlessly spamming you with 100 x 200 bytes = 20KB at 200Hz, it's 4MB/s. It will have to be quite a mighty core on a good internet connection to the peer to keep up with it. You probably want to use permessage-deflate then which also costs cpu and memory, and latency, but it will reduce the number of packets on the wire that have to make it through and compress several updates into a single packet. Considering the server side also has problems cramming all this down the wire, this is likely to give better performance and even latency overall.