Support the ongoing development of Laravel.io →
posted 9 years ago
Views
Last updated 2 years ago.
0

Not sure if I fully understand your question.

Let's separate into 2 parts. First you need a mechanism to sort of monitoring your database and signal others there's new operation in database. Second, you will need to have control on current connected clients and send appropriate signals to them. When doing this, you will also need to manage who's actually looking at your view, and update theirs accordingly.

It definitely looks like you wanted a Pub / Sub system.

Tho I am not sure if PHP has any libraries does that, the one I know that do these are from Node.js framework such as Meteor.js and Sails.js (Maybe MEAN ? as well)

You might want to take a look at this SO question http://stackoverflow.com/questions/22743812/php-websockets-mysql-pub-sub

So I guess what you could do is,

  • Setup a MySQL triggers
  • When there is any operations to the database, write a flag or so to a temp table
  • Setup a node.js server with web socket
  • Subscribe your app to node server
  • In your node server spin an even loop like every second to check with the temp table, and publish signals (as well as changes) to subscribed client whenever there is any operations done to the database.
  • On client side, subscribe to node server's port and listen and do whatever update you need to.
  • Reset the update flag
  • Repeat ...

See reference: https://ricolsen1supervc.wordpress.com/2013/11/20/using-triggers-to-keep-track-of-mysql-table-changes/

Last updated 9 years ago.
0

Ok..thanks for ur reply and solution but i thinks u misunderstand a bit... sorry...what i want is to refresh the view if theres new created data either from client(view)/server(database) side

i know ajax can do the job by using setintervel if i not mistaken.... but i dont know how to make it to work with my scripts....

another solution is again using ajax by returning newest created item in database(timestamp).. u can see that my database keep track of any created/updated data....

thanks for ur time....i hope u can help me with the simplest solution by applying ajax to my scripts...

thank you

0

Ajax with setInterval, in my opinion does not quite fit the task, especially when you have more than 1 client looking at that page (or your have similar pages that you want to show changes), you end up having them keep sending Ajax request to your database / server. The number of requests grows linearly with the number of clients / pages you get.

If your main concern is about how to "change" data of your view after you detect there is any changes to your database, here is one example,

jQuery

// We are return a promise for later use
var request = $.ajax({
  // your configs
});

request.done(function (response) {
  // For example: it returns `response: [{ foo: 'bar' }, { foo: 'bar2'} ]`
  var html = '';
  response.forEach(function (value, key) {
    html += '<div id="item-' + key +'">' + value.foo + '</div>';
  });

  $("#yourDom").html(html);
});

PS: jQuery changes DOM after its rendering which means you will have not very much control on the actual data in client browser's memory. (Tho, you could still do it using data- attribute, but it's not really that efficient and practical. )

You could also leverage on Backbone.js, Ember.js, Angular.js to quickly archive the same task. They have much better control on data flow and you should definitely look at their tutorials online.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

a7xtrex a7xtrex Joined 16 Aug 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.