6.29Long Polling

Created on Aug 17, 2026~7 min read

Long polling (Long Polling) is a technique for pushing data from the server to the client. It simulates the effect of server push by keeping the HTTP connection open until new data is sent to the client or until a timeout occurs. Long polling is an improvement over traditional polling (in which the client periodically sends requests to the server to check for new data), reducing unnecessary requests and improving efficiency.

How long polling works:

  1. The client sends a request to the server.
  2. If there is no new data on the server, the server does not respond immediately but instead holds the request.
  3. Once new data is available to send, or the preset timeout is reached, the server responds to the request and sends the data to the client.
  4. After the client processes the data, it sends a new request to the server again, repeating the process above.

Use cases for long polling:

  • Real-time notifications: for example, in an online chat application, when a user receives a new message, the server can push the message to the client in a timely manner via long polling.
  • Online collaboration tools: in applications where multiple people edit a document simultaneously, long polling can be used to synchronize users' edits in real time.
  • Game updates: in online games, long polling can be used to update game state in real time, such as player position, score, and other information.
  • Stock market updates: financial applications use long polling to display stock price changes in real time.
  • Configuration center: in a microservices architecture, the configuration center uses long polling to ensure that each service can immediately receive the latest configuration changes. When a configuration changes, the configuration center can quickly push the update to all relevant service instances, ensuring configuration consistency and timeliness.

The following example shows how to use a long polling request:

cs
await httpRemoteService.LongPollingAsync("https://localhost:7044/HttpRemote/LongPolling"    , async (responseMessage, token) =>    {        Console.WriteLine(await responseMessage.Content.ReadAsStringAsync(token));        await Task.CompletedTask;    }, cancellationToken: cancellationToken);// Using the builder patternawait httpRemoteService.SendAsync(HttpRequestBuilder    .LongPolling("https://localhost:7044/HttpRemote/LongPolling"    , async (responseMessage, token) =>    {        Console.WriteLine(await responseMessage.Content.ReadAsStringAsync(token));        await Task.CompletedTask;    }), cancellationToken: cancellationToken);

Long polling also supports consuming data as IAsyncEnumerable<HttpResponseMessage>, allowing you to use await foreach to iterate over each polling response:

cs
await foreach (var responseMessage in httpRemoteService.LongPollingAsAsyncEnumerable("https://localhost:7044/HttpRemote/LongPolling", cancellationToken: cancellationToken)){    // Note: each response must be disposed manually after use (or use using)    using (responseMessage)    {        Console.WriteLine(await responseMessage.Content.ReadAsStringAsync(cancellationToken));    }}// Using the builder patternawait foreach (var responseMessage in httpRemoteService.SendAsAsyncEnumerable(HttpRequestBuilder.LongPolling("https://localhost:7044/HttpRemote/LongPolling"), cancellationToken)){    using (responseMessage)    {        Console.WriteLine(await responseMessage.Content.ReadAsStringAsync(cancellationToken));    }}

Although long polling addresses the need for real-time communication to a certain extent, it also has some drawbacks: for example, it may place significant pressure on the server under high concurrency, and long-lived connections may affect server performance. As Web technologies evolve, more advanced techniques such as Server-Sent Events or WebSocket have gradually become the preferred choice for real-time bidirectional communication. Nevertheless, in certain constrained environments, long polling remains a viable option.