SetTimeOut and server traffic

4

I have a question. I am implementing setTimeOut() in a comments application, where every 3 seconds the div is refreshed so that new comments appear automatically without the need for refresh . It is working correctly:

setTimeout(listComment, 3000);
$("#output").html(list);

How far does it interfere with server traffic, and is there any other solution to this?

    
asked by anonymous 11.01.2018 / 14:12

2 answers

4

You need to work with WebSocket instead of making requests. Nowadays there are several libs for any backend language that help you and facilitate implementation. Here's a list with some (caso nenhuma seja a que você está usando, comenta que eu atualizo) languages:

You may also want to take a look at how Firebase , which is a backend solution for both mobile how much web.

    
11.01.2018 / 14:40
12

It interferes a lot, this should not be done in general. You need an event system (the mechanism is this, but the architecture for client / server is different).

Using the Hollywood Principle, the client registers itself on the server telling what context it is and what it wants to be notified, then the server sends something to the client when a change relevant to that subscriber happened.

The update system is only interesting when almost all requests will bring new relevant information.

Today we often use reactive programming that raises the event system to another level. There's plenty of ready to do push notification . For example, in C # you usually use ASP.NET SignalR . But you have other options .

For some, such as for PHP (I do not know if it's good) or ReactPHP .

There are API services that do this, but it does not seem like what you want.

But the basic idea is to use Web Sockets . The technique is often long polling . Also look for asynchronous notification.

Dealing with Web Sockets Direct can be pretty annoying and it's easy to do wrong, I would not waste time, unless I wanted to go deep into it. Choose a more ready solution.

This is how this site, Facebook, and many others work.

    
11.01.2018 / 14:20