Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys... i would like to ask what are the fastest/effective way on updating my database contents?
Assuming that I'm creating a chatting website where user's online can chat with their friend live just like facebook have. And one of the facebook features is that you can view all your online friends in realtime. So i was wondering on how to be able to have the effective approach to let you chat with your friends and let you know that one of your friends have login?

What im thinking on the chat system is that when i click on the send button it will update the database and assuming that i have MSSQL or MySQL as my database. So assuming that when i hit send the message will be save on my database then on the other side (the one im chatting with) they will have a function that keeps on checking the database of new message for them by their chatmate. But here is the problem. I just recently read that on "MySQL" they have this queue process where "IF" 1000 users send an update command to the database almost at the same time so the tendency is that MySQL will queue all those update command (from what i read MySQL by default can only handle about 200+ command at the same time more than that all command will be queue)...

So now the problem is if 1000 update command was send at the same time on the database it will queue the remaining 700+ user command. so my chat system will not function as expected and will not update a new message in real time because your message may be still in queue...

1.)I want to ask what are the effective way to create the realtime chat system?

2.)is there a way to create it without going through the database and keeps on updating the database content? I would really appreciate your ideas guys...


This is what im thinking of how to accomplish this... please correct me if I'm wrong

im thinking of combining ajax, jquery, php to create this chat system.. 
In my jquery/javascript/ajax code 
============================================================================= 
$(document).ready(function(){ 
  setTimeout("getMessages()",1000); 

$("#sendMessageButton").click(function(){ 
  $.ajax({ 
     type: 'POST', 
     url: 'send_message.php', 
     data: { 
        'message' : message, 
        'user_id': u_id }, 
     dataType: 'json', 
     success: function(response){ 
        if(response != ""){ 
            /* Clear send chat area */ 
        } 
     } 
   }); 
}); 

function getMessage(){ 
    $.ajax({ 
      type: 'POST', 
      url: 'get_message.php',
      data: { 
         'message_id' : id, 
         'user_id': u_id 
       }, 
      dataType: 'json', 
      success: function(response){ 
         if(response != ""){ 
            /* Append to chat box*/ 
         } 
         setTimeout("getMessages()",1000); 
      } 
   }); 
} 
});

====================================================================== 
in my get_message.php
 /*
  - Connect to database 
  - Retrieved messages needed 
  - echo json_encode(message retrieved) 
 */ 
?>
===================================================================== 
in my send_message.php 
<?php <br mode="hold" /?>/* 
  - Connect to the database 
  - Do the query update for new messages that was send by the client 
*/ 
?>



Is my approach right or do i need to modify something?
Posted
Updated 22-Sep-11 23:37pm
v2

1 solution

There is nothing wrong with storing the messages in a database, but you should never query the database to get the messages you want to show.

1) You should create a system which stores messages that should be send to each client.
2) in your webservice you should have a "GetChatMessages" method this method returns all messages that are stored for the client sending the request. If no messages are stored it waits untill a message arrives.
3) in your client you query the webservice GetChatMessages method and when you get a response you write the messages to the ui. If the connection timesout you open the connection again.

these 3 points are a simple overview of what you should do, but actually doing it is not as simple. I'm sure there is tons of open source chat systems out there, a quick google will show that I'm sure.
 
Share this answer
 
Comments
Madzmar25 23-Sep-11 5:22am    
Ahm... Correct me if im wrong im thinking of combining ajax, jquery, php to create this chat system..

In my jquery/javascript/ajax code
=============================================================================
$(document).ready(function(){
setTimeout("getMessages()",1000);

$("#sendMessageButton").click(function(){
$.ajax({
type: 'POST',
url: 'send_message.php',
data: {
'message' : message,
'user_id': u_id
},
dataType: 'json',
success: function(response){
if(response != ""){
/* Clear send chat area */
}
}
});

});

function getMessage(){

$.ajax({
type: 'POST',
url: 'get_message.php',
data: {
'message_id' : id,
'user_id': u_id
},
dataType: 'json',
success: function(response){
if(response != ""){
/* Append to chat box*/
}
setTimeout("getMessages()",1000);
}
});
}
});

======================================================================

in my get_message.php

<!--?php


/* - Connect to database
- Retrieved messages needed
- echo json_encode(message retrieved)
*/

??-->

=====================================================================
in my send_message.php


<!--?php
/*
- Connect to the database
- Do the query update for new messages that was send by the client

*/


??-->

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900