How to make a refresh more dynamic?

Asked

Viewed 58 times

1

Is there a method of fetching data from a server by directly putting it to a page without having to use a basic javascript example: ??

setInterval(function() {
    $.ajax({
       type: "POST",
       url: "retorno.php",
       data: 'id=1',
       cache: false,
       success: function(html) {
           $("div").html(html);
       }
   });
}, 1000);

Because with that kind of example I can’t make it more dynamic with the page. EXAMPLE:

If it’s to list a conversation between two people, with this code, I can’t release a notification sound to alert the new message user.

Is there any way to use this type of code to make it to handle it, because in setInterval the page would be giving refresh time to time preventing code analysis.

Type the Facebook notification where it appears to be directly on the page and only notifies when there is change on the server.

  • 2

    You can take a look at websocket, it might solve your problem. It’s better than connecting with the server every X seconds. Imagine if a waiter keeps asking every minute if the dish is ready. Terrible! The Boss is the one who notifies the waiter, This is the ideal model. Similarly your browser is ordering from the server every X seconds is very bad. Let the server notify a specific client of any changes. Use technique [Comet][1] [1]: https://pt.wikipedia.org/wiki/Comet_(program%C3%A7%C3%A3o)

1 answer

0

You can use Websocket, although indirectly the server procedure is the same, only run by a thread in the software application layer. In the case of Websocket there is a service/Thread that performs data validation and sends a return to the browser, which is waiting for the answer (Listining). An interesting link to analyze some of the Websocket alternatives (HTML5).
http://www.html5rocks.com/pt/tutorials/websockets/basics/

In this URL we find some languages in which we can instantiate our Websocket server, Java, . Net, Nodejs, among others.

Another interesting reference about Websocket
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

On the IBM website there is a nice example of chat with Websocket
http://www.ibm.com/developerworks/br/library/wa-bluemix-html5chat/index.html

Browser other questions tagged

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