Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 304
Edgar Navasardyan Avatar asked Dec 17 '16 12:12

Edgar Navasardyan


People also ask

Is long polling better than WebSockets?

Long polling is more resource intensive on the server than a WebSocket connection. Long polling can come with a latency overhead because it requires several hops between servers and devices.

Should I use SSE or WebSockets?

SSE is best used when it's not necessary to send data from client to server. For example, in status updates and push notification applications, the data flow is from the server to the client only. This is what SSE is designed for, so WebSocket would be overkill. It's always wise to use the best tool for the job.

What is the difference between long polling WebSockets and server sent events?

Differences. Obviously, the major difference between WebSockets and Server-Sent Events is that WebSockets are bidirectional (allowing communication between the client and the server) while SSEs are mono-directional (only allowing the client to receive data from the server).

When should you not use a WebSocket?

Avoid using WebSockets if only a small number of messages will be sent or if the messaging is very infrequent. Unless the client must quickly receive or act upon updates, maintaining the open connection may be an unnecessary waste of resources.


1 Answers

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.

like image 172
jfriend00 Avatar answered Sep 20 '22 07:09

jfriend00