diff --git a/mod/chat/lib.php b/mod/chat/lib.php
index ef91dc1..f16e8a9 100644
--- a/mod/chat/lib.php
+++ b/mod/chat/lib.php
@@ -566,7 +566,7 @@ function chat_get_latest_message($chatid, $groupid=0) {
 
     $sql = "SELECT *
         FROM {chat_messages_current} WHERE chatid = :chatid $groupselect
-        ORDER BY timestamp DESC";
+        ORDER BY timestamp DESC, id DESC";
 
     // Return the lastest one message.
     return $DB->get_record_sql($sql, $params, true);
