What is HTTP Long-Polling vs an HTTP Request? When would one or the other be preferred?
When a regular HTTP request is sent to an API, the client expects a reply immediately from the server.
In case of long polling, the server will not immediately send a response, it'll only send a response when an event occurs on server side (e.g. a value changes). So the client sends a requests and waits until a value changes on server side before receiving a response. After receiving a response, it can again send a request and wait again for a new update.
This can be beneficial to limit the number of requests a client needs to send to see if there's an update (while there's maybe none).
Great! Thank you for those insights.
With long-polling the client gets the response back as soon as it is available, while with a normal http-request it has to do some polling (for example every minute) to get that same response (so it could for example take a minute longer till the client has the response).
Disadvantage of long-polling is that this takes up more server-resources (the connection stays open a long time for a single request), so this is not scalable for larger applications with multiple users. A better alternative is a websocket, where the client also immediately receives the response when it is available, while the same websocket connection can handle multiple requests.