Is it possible to load the page when making changes to the database?

I'm just wondering if it is possible to reload the current page using jQuery or AJAX when some kind of database has been modified? If possible, how can I make this a reality on my website?

Demonstration of what I mean: let's say that one visitor is on the main page of the website. The visitor is simply idle on the page when I publish a new blog post. The page reloads using jQuery or AJAX when it sees a new blog post in the database and shows a new post to the visitor. The Simliar function for this is setTimeout() , but I do not want to underline the server if I have 100 active visitors on one page.

Thanks in advance.

+4
source share
3 answers

Set javascript var MyItemID = get_file_contents(notice.html) on the page, use setTimeout() for the content of the request from notice.html and compare the result with your MyItemID

Each time you update your database, get the maximum record ID through mysql_insert_id() and put it in notice.html .

Pretty easy, huh ;;)

+1
source

AFAIK you cannot allow the server to click on clients via HTTP on the server event (maybe I'm wrong here), you can use the SPDY protocol if your clients support it (I think only the Chrome browser).

So, the only thing you can do is check if the last entry matches the same as in pageload. You must check with the server every X seconds / minutes.

You can achieve this by doing something like this:

  • with pagerequest, save the identifier of the last entry somewhere on the page (for example, hidden input).
  • Write a small server-side script to return only the identifier of the last record (simple)
  • Compare them with the client, if they do not match, complete the full ajax request for new content.

The only thing the server needs to do (every X with per client) is query the database for the last record and display it on the screen. You can also check the server (more load on the server) or output it via JSON (also more load on the server).

I do not think you can do this without these checks.

0
source

Install javascript

 var MyItemID = get_file_contents(notice.html) 

on the page, use setTimeout() to query content from notice.html and compare the result with your MyItemID

Each time you update your database, get the maximum record ID through mysql_insert_id() and put it in notice.html .

0
source

Source: https://habr.com/ru/post/1384225/


All Articles