Best practices for timeout in HTTP long polling

As an avid user of Javascript AJAX and long-polling, I am constantly seeking the best value for server response timeout.

Despite scouring numerous documents, a detailed explanation for timeout continues to elude me.

Some suggest 20 seconds, while others lean towards 30 seconds...

I implement logic similar to that depicted in the diagram https://i.sstatic.net/Iinn9.jpg

How do I determine the optimal value for timeout?

Would 5 minutes be too excessive? Is this considered normal practice?

PS: Possible Ajax client internet connections include Ethernet RJ-45, WiFi, 3G, 4G, as well as those utilizing NAT and Proxy servers.

I am concerned about third parties potentially dropping the connection due to extended timeouts in certain scenarios.

Answer №1

Perhaps your understanding of the English language is causing confusion, but it's actually the duration of the connection (the time between opening and closing the connection) that you should be more concerned about rather than the timeout period (the length of time without activity after which the connection will be terminated).

Even with the availability of websockets, there are still many instances of hardware dropping connections regardless of activity or looking for inactivity, especially when traffic appears to be HTTP or HTTPS. This could be due to design flaws or as a way to mitigate slowloris attacks. Having clients using 3G and 4G may lead to issues with a 5-minute lifespan.

Unfortunately, there isn't a one-size-fits-all solution for determining what connection duration will work universally. It's crucial to consider where your users are located. If they are all within your LAN and connecting directly to the server, you can likely use a longer duration. However, setting the duration to unlimited may expose any memory leaks in your application - sometimes it's better to refresh periodically anyway.

In cases where there is infrastructure beyond just hubs and switches between your server and clients, you'll need a way to detect and re-establish dropped connections regardless of how long they've been inactive. Once you figure this out, you can then:

  1. See dropped connections as a minor performance hiccup that doesn't significantly impact functionality

  2. Easily implement logging for dropped connections to identify the optimal connection time and address the small issue mentioned in (1)

Answer №2

Your communication skills in English are excellent.

Summary - Timeouts range from 5-30 seconds based on user familiarity.

I recommend setting long poll timeouts to be 100 times the server's "request" time. This argument supports timeouts between 5-20 seconds, depending on how quickly you need to detect dropped connections and missing clients.

Reasoning Behind This Recommendation:

  • Many examples use timeouts of 20-30 seconds.
  • Most routers will quietly terminate connections that remain open for too long.
  • Clients can suddenly vanish due to network issues or entering low-power mode.
  • Servers cannot identify dropped connections, thus a 5 minute timeout is not advisable as it ties up sockets and resources, potentially leading to a DOS attack on your server.

Hence, a timeout of less than 30 seconds is considered standard. How should you decide?

What are the advantages and drawbacks of having long-poll connections?

Assuming a typical request takes 100ms of server "request" time for connecting, querying a database, and sending a response:

A 10-second timeout amounts to 1% of the long-polling time. (100 / 10,000 = .01 = 1%)

A 20-second timeout translates to 0.5%

A 30-second timeout equals 0.33%, and so on.

Any longer than 30 seconds, the practical benefit of extending the timeout further will always be less than a 0.33% increase in performance. Therefore, there is little justification for timeouts over 30s.

Conclusion:

It is recommended to set long poll timeouts at 100 times the server's "request" time, advocating for timeouts within the range of 5-20 seconds depending on the urgency to detect disconnected clients.

Best practice: Sync both client and server timeouts for requests. Leave extra network ping time for the client's safety. For instance, server = 100x request time, client = 102x request time.

Best practice: Long polling proves superior to websockets in many scenarios due to its simplicity, scalability, and reduced HTTP attack surface.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a NPM error when trying to launch the server using ng serve

I encountered an error : ERROR in /opt/NodeJS/FutureDMS/src/app/app.module.ts (5,9): Module '"/opt/NodeJS/FutureDMS/src/app/app.routing"' has no exported member 'APP_ROUTE'. Within my code, I have utilized arrow function in the loadCh ...

The local variable within the Angular constructor is not initialized until the ngOnInit() function is invoked

I am encountering difficulties with making backend calls from Angular. In my component, I am fetching the "category" parameter from the URL as shown below: export class ProductsComponent{ productList = [] category = "" $params; $products ...

JQUERY confirm dialog causing AJAX malfunction

I am encountering an issue where I am trying to execute an ajax function only after the user confirms a dialogue using JQUERY confirm. Strangely, when I include the confirmation step, my code throws an error and does not function properly. It's worth ...

The method by which JavaScript identifies when a Promise has resolved or rejected

How does JavaScript determine when the state of myPromise has transitioned to "fulfilled" in the code provided below? In other words, what is the process that determines it's time to add the .then() handler to the microqueue for eventual execution? co ...

Clicking on an Angular routerLink that points to the current route does not function unless the page is re

Currently, I am working on an E-commerce project. In the shop page, when a user clicks on the "View More" button for a product, I redirect them to the product details page with the specific product id. <a [routerLink]="['/product-details' ...

Is there a way to retrieve the filename of a file uploaded using a shiny fileInput function?

This shiny app has a feature where users land on the 'Upload data' panel upon launching. To restrict access to the other two 'tabpanels', users must first upload both necessary files in the 'Upload data' tab. The condition for ...

The function Waterline.create() is not available

Currently in the process of building a basic REST API using Sails.js and Waterline-ORM, I've encountered an issue regarding Post.create is not a function when trying to create an object within the ORM on a Post request. Here is my model: module.expor ...

Ways to convert an asynchronous operation to synchronous in JavaScript

Currently in the process of developing an eslint plugin, I have come across a particular issue. My goal is to implement real-time changes to the configuration file by making an HTTP request to retrieve the JSON configuration. When attempting to execute co ...

What are the ways to activate an element in vue js?

Is there a way to modify the code so that the function triggers with just one click instead of two? export default { methods: { remove(){ $('.remove-me button').click( function() { removeItem(this); }); ...

Tips on how to retrieve the value of the second td in an HTML table when clicking on the first td utilizing jQuery

There is a specific requirement I have where I must retrieve the value of the second td in an HTML table when clicking on the first column. To accomplish this task, I am utilizing jQuery. $('.tbody').on('click','tr td:nth-child(1) ...

When using the onclick function, an unexpected behavior occurs where instead of displaying content, the page

Below is a summarized version of my code: HTML <a onclick="showHideDiv('bio')" target='_self'> bio <div id="bio" class="shadowScreen" style="display: none;"> <p class="showBio"> Bio! </p> &l ...

Capture the selected hyperlink and show the corresponding page title in a designated box for reference

I want to track the links that users click on and display them in a box with an image and name of the page. Additionally, I would like to show how long the user spent on each page below the image. The images in the box should also be clickable links to the ...

Transfer the document into JavaScript by copying and pasting

I am in need of some assistance with a basic form that includes an input field. While the form is functional, I would like to provide users with the option to upload a file by simply pasting it into the browser using CTRL+V. Is there a way to achieve this ...

There seems to be a mysterious absence of chuck norris jokes in

I'm attempting to call two different APIs by clicking a button to display a random joke on the screen. I've created two separate functions to fetch the APIs and used Math.random() to generate a random number. If the number is greater than 50, one ...

Using JavaScript to launch a new window for a specific folder

When opening a popup window with a specific URL, I typically use the following code: $("#OpenFolder").click(function () { var url = "https://stackoverflow.com"; windowObjectReference = window.open(url, "ModulesList", " ...

The binding in Knockoutjs is working properly, but for some reason the href attribute in the anchor tag is not redirecting to

Here is the HTML code snippet I am working with: <ul class="nav nav-tabs ilia-cat-nav" data-toggle="dropdown" data-bind="foreach : Items" style="margin-top:-30px"> <li role="presentation" data-bind="attr : {'data-id' : ID , 'da ...

Activate the event when the radio button is changed

Is there a way to change the selected radio button in a radio group that is generated using a for loop? I am attempting to utilize the changeRadio function to select and trigger the change of the radio button based on a specific value. <mat-radio-group ...

Angular.js page failing to reflect changes following Firebase request completion

myApp.controller('RegistrationController', ['$scope', function($scope) { var auth = firebase.database().ref(); // console.log("auth::"+auth); $scope.login = function() { $scope.message = "Welcome " + $scope.user.ema ...

Having trouble with NPM install because of a node-gyp issue?

We're facing issues with running "npm install" on our project. The error message states that a specific file cannot be located: fatal error C1083: Cannot open include file: 'windows.h' This error seems to be originating from the node-gyp mo ...

Creating a bare JSON request in Express 4.13 using the body-parser middleware

My Express server is set up with the following parameters and routes: var express = require('express'); var bodyParser = require('body-parser'); var router = express.Router(); var app = express(); app.use(router); app.use(bodyParser.ur ...