Long-polling vs websocket when expecting one-time response from server-side
Asked Answered
M

2

11

I have read many articles on real-time push notifications. And the resume is that websocket is generally the preferred technique as long as you are not concerned about 100% browser compatibility. And yet, one article states that

Long polling - potentially when you are exchanging single call with server, and server is doing some work in background.

This is exactly my case. The user presses a button which initiates some complex calculations on server-side, and as soon as the answer is ready, the server sends a push-notification to the client. The question is, can we say that for the case of one-time responses, long-polling is better choice than websockets? Or unless we are concerned about obsolete browsers support and if I am going to start the project from scratch, websockets should ALWAYS be preferred to long-polling when it comes to push-protocol ?

Moberg answered 17/12, 2016 at 12:1 Comment(6)
I don't think you should do either. If you make an API call that triggers a long-running action, then return 202 Accepted with a Location to retrieve the result from. See e.g. farazdagi.com/blog/2014/rest-long-running-jobsVivianna
@Vivianna - The client still needs a strategy for knowing when the long-running action is complete and when it can get the result (which is the main point of the OP's question). Your suggestion does not solve that.Orangeism
@Vivianna - And, that's what the OP is asking. Should they use polling, long-polling or a webSocket to get such a response. Your comment (or the article you link to) doesn't direct them to an answer to that part of the question in any way.Orangeism
@Vivianna - You suggested a third option that doesn't solve the push part of the question at all. The article you link to is an architectural idea and is lacking some specifics needed for an implementation. In particular, it says nothing about the strategy the client should use to know when the long-running result is finally ready for retrieval. And, that is exactly what the OP is asking about. I'm commenting that your suggestion and that article is incomplete in that regard and that is a key part of this question.Orangeism
What is the range of times needed for a response from your complex server-side calculations? Seconds? Minutes? Hours? If it's seconds, then I'd just use an Ajax call and just send the ajax response when the calculation was done. If it's long enough that you can't do that with a single ajax call, then I'd probably use a webSocket because that's likely to be more efficient than any http polling implementation.Orangeism
ok .Got your points. It's 5-10 minutesMoberg
O
18

The question is, can we say that for the case of one-time responses, long-polling is better choice than websockets?

Not really. Long polling is inefficient (multiple incoming requests, multiple times your server has to check on the state of the long running job), particularly if the usual time period is long enough that you're going to have to poll many times.


If a given client page is only likely to do this operation once, then you can really go either way. There are some advantages and disadvantages to each mechanism.

At a response time of 5-10 minutes you cannot assume that a single http request will stay alive that long awaiting a response, even if you make sure the server side will stay open that long. Clients or intermediate network equipment (proxies, etc...) just make not keep the initial http connection open that long. That would have been the most efficient mechanism if you could have done that. But, I don't think you can count on that for a random network configuration and client configuration that you do not control.

So, that leaves you with several options which I think you already know, but I will describe here for completeness for others.

Option 1:

  • Establish websocket connection to the server by which you can receive push response.
  • Make http request to initiate the long running operation. Return response that the operation has been successfully initiated.
  • Receive websocket push response some time later.
  • Close webSocket (assuming this page won't be doing this again).

Option 2:

  • Make http request to initiate the long running operation. Return response that the operation has been successfully initiated and probably some sort of taskID that can be used for future querying.
  • Using http "long polling" to "wait" for the answer. Since these requests will likely "time out" before the response is received, you will have to regularly long poll until the response is received.

Option 3:

  • Establish webSocket connection.
  • Send message over webSocket connection to initiate the operation.
  • Receive response some time later that the operation is complete.
  • Close webSocket connection (assuming this page won't be using it any more).

Option 4:

  • Same as option 3, but using socket.io instead of plain webSocket to give you heartbeat and auto-reconnect logic to make sure the webSocket connection stays alive.

If you're looking at things purely from the networking and server efficiency point of view, then options 3 or 4 are likely to be the most efficient. You only have the overhead of one TCP connection between client and server and that one connection is used for all traffic and the traffic on that one connection is pretty efficient and supports actual push so the client gets notified as soon as possible.

From an architecture point of view, I'm not a fan of option 1 because it just seems a bit convoluted when you initiate the request using one technology and then send the response via another and it requires you to create a correlation between the client that initiated an incoming http request and a connected webSocket. That can be done, but it's extra bookkeeping on the server. Option 2 is simple architecturally, but inefficient (regularly polling the server) so it's not my favorite either.

Orangeism answered 17/12, 2016 at 21:3 Comment(2)
perfect answer. Thank you for this expertiseMoberg
Re: efficiency. Newer versions of HTTP AFAIK make long polling as efficient as web sockets for the described scenario because of request interleaving.Luiseluiza
T
4

There is an alterternative that don't require polling or having an open socket connection all the time.

It's called web push.

The Push API gives web applications the ability to receive messages pushed to them from a server, whether or not the web app is in the foreground, or even currently loaded, on a user agent. This lets developers deliver asynchronous notifications and updates to users that opt in, resulting in better engagement with timely new content.

Some perks are

  • You need to ask for notification permission
  • Your site needs to have a service worker running in foreground
  • having a service worker also means you need to have SSL / HTTPS
Trustbuster answered 10/5, 2017 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.