3

Good day,

Let me describe my problem :

Scenario :

  • User 1 has a coldfusion webpage, containing 6 alerts as html elements populated using a database. User 1 is constantly monitoring this page for changes.
  • A user on a different computer (different session) logs into a admin console and add's a 7th element and inserts it into the database.
  • The database contains a field that changes from 0 to 1 as soon as an alert is added and inserted into the database.
  • The webpage of User 1 not has to dynamically refresh or update with the 8th alert.

Problem :

  • I am struggling to run a async loop that queries the database permanently for the record that tells it there is a change, while not freezing the rest of the page.

Solution :

  • I need to run a cfquery to check the isUpdated field in the database.
  • That cfquery needs to be run every minute.
  • As soon as the cfquery returns a 1, the page should refresh which in turn will populate the new alert as well.
Simon Erasmus
  • 134
  • 12

1 Answers1

5

Can be done through sockets, and you do not have to check the availability of a new record every minute.

You can also do it with javascript/jquery:

<script>
var lastAlert = '#lastAlert#'; //last at the time of loading the page

setInterval(function() {
    lastAlert = isUpdated(lastAlert);
}, 60000);

function isUpdated(lastAlert){
res = lastAlert;
$.ajax({                            
    url: 'checkAlerts.cfm', //in this file do check for changes in the database. should return the number of the last alert
    type: 'POST',
    data: {lastAlert:lastAlert},
    cache: false,
    success:function(res){  
        if(res > lastAlert){
            //your code if there is a new entry 
            alert('a new entry has been added');
        }
    }
});
return res;
}   
</script>

I did not check the code! But I hope you understand how to proceed.

Ilya Yaremchuk
  • 2,007
  • 2
  • 19
  • 36
  • Would you want to run a db query every interval, or send a push notification when an update happens. I haven't done a lot with WebSockets, but I thought push notifications was something they were very good at, and this issue kinda describes the need for a push notification. – Shawn Jan 23 '19 at 19:35
  • Just re-read your answer, and I think I misinterpreted it. It looks like you are giving the two options of push-notification through WebSocket and jQuery scheduled requests. – Shawn Jan 23 '19 at 19:37
  • 1
    Just to expand on this. A websocket is a push technology and the jQuery option is a polling technique which is essentially a pull methodology. Polling can use up network resources, so I would opt for a websocket. You could use socket.io + nodeJS to achieve a similar outcome, if you have an older version of Lucee or ACF. – Charles Robertson Jan 23 '19 at 23:12
  • 1
    Web sockets are not only push notifications. Also used for real-time applications, chat applications .... – Ilya Yaremchuk Jan 24 '19 at 07:04