Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:10:57+00:00 2026-05-28T01:10:57+00:00

I’m building a live chat application with rooms for my assignment. Everything is going

  • 0

I’m building a live chat application with rooms for my assignment. Everything is going great so far, but I’m having trouble properly retrieving the latest chat messages from the database. The messages table has the following fields:

| m_id | u_id | r_id | message | sent |

m_id is the primary key. u_id and r_id are foreign keys to other tables to reference the user who sent the message and the room they sent the message to. The sent field has data in PHP’s microtime(true) format:

1326174129.977

I’ve built my AJAX script to retrieve messages from the database and it looks like this:

function getMessages() {
    var d = new Date();
    $.post("/ajax/getmessages.php", {roomID: roomID, timestamp: timestamp}, function(resp) {
        resp = $.parseJSON(resp);

        for(x in resp) {
            $('#chat-holder ul').append('<li>[' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + '] <strong>' + resp[x].u_id + '</strong>: ' + resp[x].msg + '</li>');
            $("#chat-holder").scrollTop($("#chat-holder")[0].scrollHeight);
        }
    });
}

The getMessages function runs once a second, and then when it retieves data back from the getmessages.php script it loops through the JSON encoded response and ads list items to the chat frame. Pretty simple.

The problem is, it retrieves all messages sent to the room, not just the ones that have been sent since the last time the user polled for the latest messages. So I end up with every message that’s been sent in the chat being added to the chat frame every second.

The only thing I’ve come up with is to store a timestamp of the last time the user requested the messages, and then use that in my backend PHP script somehow. Unfortunately, I can’t quite get my head around how to implement it.

Any help is greatly appreciated, thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:10:58+00:00Added an answer on May 28, 2026 at 1:10 am

    Given that the idea isn’t so much to get all messages after a given time so much as to get all messages since the previous batch of messages, I think it’d be easier to do this based on your message table’s primary key rather than the timestamp field. Create a client-side variable to note the highest (known) id so far:

    var highestMessageId = 0;
    

    Then when you make your ajax request pass that id through to the PHP:

    $.post("/ajax/getmessages.php", {roomID: roomID, fromMessageId : highestMessageId},
    

    Then in your PHP:

    SELECT * FROM messages
    WHERE r_id = $r_id
    AND   m_id > $m_id
    

    In the ajax success handler you then update highestMessageId with the id from the last record in the new batch.

    Of course this assumes that your table uses a standard numeric key where records added later have a higher key than records added earlier…

    EDIT: Now that I think about it exactly the same procedure would work with the timestamp field too. When I started writing the above I thought there was some reason why timestamp wouldn’t work, but I think I was just confusing myself worrying about a non-issue (I was thinking about the times of the ajax requests rather than the timestamp on the records).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.