Is it safe to use ajax requests over and over again?

Asked

Viewed 1,270 times

6

I want to create a 'mini server' to use on my site (tumblr), for real communication with my visitors and one way to do this is by using ajax requests. When the site loads, it requests a JSON file, when I receive that file the request is made again and so on. When I update the JSON file, the site receives it almost immediately, then the data will be updated for visitors.

I have doubts about doing this, I don’t know if you can crash the site, get a lot of internet or have some bad effect. You think I can do this or there’s some better way?

  • This "real communication" of which you comment, would be a kind of chat?

  • Chat no, more yes a notification on the site, to informative, not exactly a chat.

  • 1

    Let me see if I understand, your idea is that you have for example a message on the site, that when you edit on your server, it in real time appears to anyone who visits the site, correct?

  • 1

    It would not crash the site, it would spend a lot of bandwidth on both sides (client and server), which is bad for those who use limited bandwidth. But if the interval between requests is anything more than 30 seconds, for example, I imagine it’s even acceptable.

  • Yes @Kazzkiq, but in case I would update a file on Google Drive even (rsrsrsrsrs).

  • 1

    The problem is that making an ajax request every X seconds is relative to the client, that is, if 500 people enter the site, 500 simultaneous requests would be triggered every X seconds, and in terms of performance this is quite complicated. Your case seems to be typical of problems where the solution would be to use Ajax Polling or Comet.

  • 1

    Ajax pooling is what he is talking about. Comet is the junction of various techniques. In case he would have to use pooling Ajax because he intends to use Google Drive as hosting. However, this would create a problem since a bandwidth expenditure above normal could cause problems regarding the service use limit.

  • 1

    I would recommend that he use his own notification service or, if he wanted more control, use the Firebase. It uses Websockets, which makes response time minimal and its free version supports up to 50 concurrent users.

  • because then, I discovered Firebase a few days ago, I thought of several advantages of using it, but I was disappointed to learn that it only supports 50 users, so I thought of this idea of using ajax.

  • You want cheap or you want good?

  • kkkkkkkkkkkkkkkkkkkkkkkk I want the 2 u.u

  • When you mentioned that you are using Tumblr and Google Drive we consider that you will not pay for hosting. There are free, limited and efficient accommodations; free, not limited and not efficient; and paid for of all kinds.

  • 1

    Tumblr, Google Drive and most of the good free accommodations are of the first kind, so even if you use one $.ajax loading a file something may go wrong. I know some accommodation of the second type, but I do not recommend it. The third kind do not know for I am poor.

  • kkkkkk because then, I have no money to be using these things, so I thought to use them themselves =P. But it is a good or not to use this method? There’s another service you recommend?

Show 9 more comments

1 answer

4

Everything will depend on the standard size of your answer and the performance of the query. The ideal is to traffic only the data that will be consumed. You can, for example, pass in each request the ID of the last conversation the user searched, and bring the following from that.

One way to do this is to use a javascript function that each x seconds retrieve the data. However, it is important that new requests are made only after the end of the last ones - so it is not advisable to use setInterval, since, if the request takes more seconds to be processed than the query trigger interval, you get a chain of requests. You guarantee this through recursion in a non-animal self-executable function:

(function loop() {
    setTimeout(function() {
        // Inteligência da requisição
        if (complete)
            loop(); // Executa novamente a função quando a requisição atual terminar. Pode ser o **complete** do jQuery.ajax, por exemplo
    }, 5000); // Função, intervalo em milissegundos
})();
  • 1

    I was going to do it anyway, only without the break, so when the request is over it already requests a new one. More I will do using the same setTimeout

  • 2

    With the setTimeout you don’t punish your server so much... Many subsequent queries can trigger hundreds of requests per second to your server from a single client, if the return is quick. Not so problematic as user wait 5 seconds for new messages.

  • 1

    Apple, did you feel missing keys? (sorry if this is your code style)

  • For a few days I thought it was a criticism of not using keys. Now I understand the relationship!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.