+ 4

How can I always keep my data from the database on a website up to date

I'm currently working on a website that displays data from my database. For information: I use an apache web server, php to provide the data and html css and javascript to implement the front end. I would now like to keep my data on the website up to date. In other words, if the data in the database changes, I want it to change on the website as well. My working solution is as follows: I use JQuery Ajax to call my data from the database every few seconds and compare this with my previously saved values ​​from the ajax call before and if these data do not match, I update my elements on the website. This also works and is great because I can update elements without reloading the website, but the question now arises whether I really have to make an ajax call every second in order to compare the data with the previous call to see a change in data of the database can be identified thank you in advance

18th Feb 2022, 3:02 PM
SupaMario
36 Answers
+ 4
Node js with express for server, checkout graphql as well For client you can pick whichever frontend framework is best for you but you can start with Vue
18th Feb 2022, 4:10 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 4
Mario Dumfart There are two types of website: 1 - static website means fixed data 2 - dynamic website means content will change according to data. and your answer is in 2nd type of website.
18th Feb 2022, 4:32 PM
A͢J
A͢J - avatar
+ 4
Mario Dumfart I don't think it is good idea to compare with previous saved data. If there are 1000 records then think how much time it will take to compare. There might be 10 thousand, 20 thousand, 1 lac data so comparing with previous saved data might consumed lots of time and resources which is not good. Simple thing is don't think what changes has been done just display data.
19th Feb 2022, 3:35 AM
A͢J
A͢J - avatar
+ 4
If you want to get data every time when a change is done in database, for that you have to fetch data in an interval which might cause problem for medium or large data in db. In case you can use websocket.
20th Feb 2022, 3:13 PM
Sarowar Alam
Sarowar Alam - avatar
+ 3
Mario Dumfart One question why you are comparing with previous saved data?
18th Feb 2022, 4:49 PM
A͢J
A͢J - avatar
+ 2
I dont know how ca you do it with apache but first you need a web socket server and the client would connect to that socket server and it will listen for message event like socket.on('message' , callback func here) in javascript
18th Feb 2022, 4:36 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 2
Yeah that's true, but in my project it's ok i think cause it's not much data, but overall you are right
19th Feb 2022, 9:13 AM
SupaMario
+ 1
Maybe search for pub/sub subscriptions PHP, but using node.js but it is better to use node.js
18th Feb 2022, 3:17 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
Thanks for the answer! I already thought about that... You mean it's better because with nodejs i can easily do a server/client and a client/server communication, and the server sends a message to client when data changes in database, right? With apache it's difficult and i can just do it like i already did, with a constantly ajax Polling call from the client side, am i right?
18th Feb 2022, 3:31 PM
SupaMario
+ 1
And one more question 😅: i heard something about angularJS, but this is just a framework for frontend right? So i would also need ajax calls when i use angularJS? Cause i always think that the client can recognize a change of the data in database but that's wrong right? Just that you know: I am student and kind a new to those topics
18th Feb 2022, 3:36 PM
SupaMario
+ 1
Well dont go with angular at angular at first 😅
18th Feb 2022, 3:41 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
First you have a server api read for that just sends data to the client in json format Read for rest API and the client can get this data from the server using http post look for fetch API
18th Feb 2022, 3:43 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
And check out Vue its frontend framework or svelte, or if you want Angular
18th Feb 2022, 3:45 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
https://morioh.com/p/0fdc475586ee Check this out how the project structure goes
18th Feb 2022, 3:48 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
Bro thanks a lot for those informations, it will really help me in the future. I really appreciate. But can you answer this question from before --> "Thanks for the answer! I already thought about that... You mean it's better because with nodejs i can easily do a server/client and a client/server communication, and the server sends a message to client when data changes in database, right? With apache it's difficult and i can just do it like i already did, with a constantly ajax Polling call from the client side, am i right?"
18th Feb 2022, 3:53 PM
SupaMario
+ 1
Doings and it like that with ajax polling is a bad idea because the client constantly sends requests, and i prefer node js because its faster and its javascript, instead of constantly polling checkout sockets
18th Feb 2022, 4:02 PM
Мартин 😑🎵
Мартин 😑🎵 - avatar
+ 1
Ok i understand. So in your opinion nodejs and a frontend Framework like Vue would be the best option?
18th Feb 2022, 4:07 PM
SupaMario
+ 1
Ok thank you! Is there any way to do it with apache/php? May with websockets or Server sent events? Or for what is apache commonly used? Not for something like that i guess
18th Feb 2022, 4:19 PM
SupaMario
+ 1
Yeah sure, but i was always talking about dynamic, i hope i explained my problem sufficiently
18th Feb 2022, 4:35 PM
SupaMario
+ 1
Ok thanks a lot men i appreciate
18th Feb 2022, 4:44 PM
SupaMario