A
A
Anton Ivanov2020-05-08 14:21:17
JavaScript
Anton Ivanov, 2020-05-08 14:21:17

When should I use ajax and when should I use websocket?

There is a script which sends request and should receive the answer.
He can get an answer (say) within 5 minutes.

The question is, from what considerations should one proceed when choosing between "ajax request to the server once every N seconds" or "opening a websocket"?

In my opinion (non-professional front-end developer), a web socket is preferable, since the client will instantly receive a response as soon as it appears on the server, and this can be pleasant for the user, since the appearance of data on the server is the result of an action in the user’s mobile application and is pleasant (although not critical) to see the result immediately, and not after N seconds.

There will be exactly one response from the user, that is, after receiving it, the script exits.

I would like to hear the negative sides of the websocket

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
ThunderCat, 2020-05-08
@Fly3110

I would like to hear the negative sides of the websocket

In this vein, there are practically none, except that the server does not have the ability to organize a socket. Otherwise, this is actually what it was invented for, queries by interval are the worst possible option.

U
uvelichitel, 2020-05-08
@uvelichitel

The negative side of the websocket is the lack of delivery guarantees. The package may get lost, the connection may break, and the parties will not notice. ajax provides delivery guarantees at the protocol level.

D
Dmitry Tyvaniuk, 2020-05-09
@dmitry0141e

It makes no sense for you to use sockets for 1 request. On ajax, you can not only make requests by interval, read about long polling

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question