Real time ajax request

Asked

Viewed 1,763 times

2

Well personal I have a script to request ajax every 1 second to show me new content but now I’m having a problem that it every 1 second updates me all the content I wanted that only shows up if there is new content in the database.

The script I’m using and this

<script>
//LISTA POSTS ESTABELECIMENTOS
    $(document).ready(function() {
    $.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
    setInterval(function() {
    $('#mostra_posts').load('ajax/mostra_posts.php?id_estabelecimento=<?php echo $row->id; ?>');
    }, 1000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
    });
    // ]]>
</script>

I wonder how I can make use of this script.

  • If you want the right way/recommended/rapido http://www.html5rocks.com/en/tutorials/websockets/basics/

  • You have to know on the server that the content has changed?

2 answers

2

You are using a technique called pooling which is to keep asking the server from time to time if you have a new content, this in itself is already heavy and if every time your request hits the server it makes a bank reading will become even heavier, That might overload your server a little. If this happens consider the use of websockets. Follow two links that may be helping the subject client socket and server socket

  • i nunc worked with websockets wanted to implement because I will have a system if followers with notifications but for what I saw it is complicated

  • Have you considered using other technology than PHP? With Nodejs this is very simple.

  • but using nodejs would stop using PHP ?

  • the problem is that I’ve never used it and I don’t even know where to start or how to install it ?

  • You have to use both technologies in the same application, you have to work with two servers, and communicate between the servers. It’s a good solution only more complicated.

  • but I now have the project almost finished so I would have to do all over again to use nodejs that suggests me then not to change that

  • In this case you can continue using pooling or try to implement the socket server in php

  • is talking about Ratchet

  • Yes, there are many variables that you have to consider to make this decision, you may for example think that the way it is the server will support quietly and that implementing Ws would waste time. Only you will be able to decide what is most appropriate. I’m just showing you have other means and their advantages.

Show 5 more comments

1

The ideal would be to create another ajax service that notifies if there is a change or not, if the change is detected only then make the request to update the new content.

an example would be:

var auto_atualiza = setInterval(function () {
  $.get('possui_alteracao.php', function(data) {
    if (data.possui) {
      $('#meudiv').load('listadados.php');
    }
  });
}, 30000);

if you do not wish to take a different approach, it would be to check whether there is a change in the FOD, in that way:

var cachedDOM = $('#meudiv').html();
var auto_atualiza = setInterval(function () {
  $.get('listadados.php', function(data) {
    if(data != cachedDOM) {
      $('#meudiv').html(data);
      cachedDOM = $('#meudiv').html();
    }
  });
}, 30000);

If you need more advanced DOM monitoring features follow a reference https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

  • I did with this test but it always updates the entire content and not the new content in the database not updated the remaining data

Browser other questions tagged

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