I am creating a PHP/AJAX chat client. The application makes an AJAX request to a PHP script which gets the contents of an XML file, and responds with the contents. The client then parses the XML and displays the chats. The XML file contains all the chats, but to save server resources, I would only like the PHP script to return new chats, instead of all the chats. Here is my XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<chat>
<message>
<hour>12</hour>
<minute>06</minute>
<half>AM</half>
<day>Saturday</day>
<month>March</month>
<date>19th</date>
<body>Hey Guys!</body>
</message>
<message>
<hour>12</hour>
<minute>08</minute>
<half>AM</half>
<day>Saturday</day>
<month>March</month>
<date>19th</date>
<body>How are you?</body>
</message>
<message>
<hour>3</hour>
<minute>28</minute>
<half>PM</half>
<day>Saturday</day>
<month>March</month>
<date>19th</date>
<body>Good, thanks!</body>
</message>
</chat>
The <message>
tag defines each new chat, the tags <hour>
, <minute>
, <half>
, <day>
, <month>
, and <date>
define the time each chat was sent, and <body>
contains each chat.
This is the solution I have thought of:
- Adding a tag to the XML file that tells what number each chat is:
<number>12</number>
- In the AJAX request, having the client send the latest chat's number to the PHP script to check if it is the latest, and then having PHP continuously check the XML file for a
<number>
tag with a number greater than that, and then returning the chat when there is a number greater.
I don't know how well this would work, is there a better way to do this? Again, I just want the PHP script to return new chats as they come. Could someone please help me out or point me in the direction of something that might work? Thanks!