Settimeout and server traffic

Asked

Viewed 152 times

3

I have a question. I am implementing the setTimeOut() in a comments application, where every 3 seconds the div is updated so that new comments appear automatically without the need to refresh. Is working properly:

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

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

2 answers

12

It interferes a lot, generally this should not be done. You need a event system (the engine is this, but the client/server architecture is different).

Using the Hollywood Principle the client registers on the server telling what context they are in and what they want to be notified, then the server sends something to the client when a relevant change to that subscriber has happened.

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

Today it is customary to use reactive programming that elevates the event system to another level. There are many things ready to do the push notification. For example, in C# it is customary to use ASP.NET Signalr. But there other options.

To have some, like for PHP (I don’t know if it’s good) or Reactphp.

It has API services that do this, but it seems that is not what you want.

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

Dealing with direct Web Sockets can be pretty boring and it’s easy to do wrong, I wouldn’t waste time unless I wanted to get into it. Prefer a more ready solution.

That’s how this site, Facebook, and several others work.

  • Hello Maniero. In our case we will use PHP.

  • @Fox.11 gave an improved

4


  • 1

    Hello William. Let’s use PHP. I’ll take a look at the links reported.

Browser other questions tagged

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