943,169 Members | Top Members by Rank

Ad:
Sep 1st, 2010
0

Live Messaging

Expand Post »
I am trying to set up a sort of instant messaging system on a site I am creating. Users submit their messages which are then stored in a database. Other users looking at the page should then be able to see this message as soon as possible.

The only way I can make this work is using Ajax to send a request to search for any new messages every 2 seconds or so. Is there a better way of doing this? I just don't think this is very good as for the user's Internet usage and it is barely instant.

Thanks if anyone has any ideas...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 2nd, 2010
0
Re: Live Messaging
I also trying this . But anyone can help me .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jibon57 is offline Offline
5 posts
since Aug 2010
Sep 4th, 2010
0
Re: Live Messaging
Anyone have any ideas?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 7th, 2010
0
Re: Live Messaging
I'm sure someone must know a more efficient method?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 7th, 2010
0
Re: Live Messaging
i am also trying to make a chat app using flash and php ...but i am facing different problem ...my users are out from flash and passing username through JavaScript they can chat...but i cant display online users in realtime ....
how are you displaying users...
Last edited by hotice47; Sep 7th, 2010 at 6:59 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hotice47 is offline Offline
23 posts
since Dec 2009
Sep 7th, 2010
0
Re: Live Messaging
Use jQuery .load() method to a PHP script? I use it to simulate frontend applications monitoring real time data from machinery. If anyone wants it i can post a bit of code up...
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
benhowdle89 is offline Offline
81 posts
since Jun 2010
Sep 7th, 2010
0
Re: Live Messaging
Hi benhowdle,

that is kind of how I'm doing it at the moment. But it just means I (or rather the user) have to keep on .load()ing the PHP script. Which means that after 10minutes you could have made hundreds, maybe a thousand load()s, and if you have a thousand people messaging it would just become very slow and the user ends up with a heap of XHR documents.

This is one method that does work, but there must be a better way, maybe. Like a way to send the information to the user when it is first available (which is impossible I know, but something similar), instead of them having to request information every second or so.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 7th, 2010
0
Re: Live Messaging
http://niryariv.wordpress.com/2009/0...at-with-comet/

I see what you mean, i will keep trying and see if i come up with anything. Just found that link above and it goes on about a sort of "reverse-ajax" which sounds interesting...
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
benhowdle89 is offline Offline
81 posts
since Jun 2010
Sep 8th, 2010
0
Re: Live Messaging
Thank you for the link ben.

I think I am getting closer to a better method.

Now the user sends one request, this request is held by the PHP script using the sleep() function until any new messages are found it then sends the messages back to the user if there are any. The only problem is that when this request is being held by PHP the user cannot send any more message for some reason..

I am only sending two requests... receive and send.

Any way here is my code (mostly) below if someone can help me fix this.

Javascript (using jQuery):
javascript Syntax (Toggle Plain Text)
  1. function send(message){//Send message
  2. if(message&&message!='\n'){//If there is a message
  3. $.post('send.php','m='+message+'&t='+topic,processSend);//Request post.php
  4. }
  5. }
  6.  
  7. function processSend(data){//When send.php returns
  8. if(data!=1){//send.php returns 1 on success
  9. $('#send_error').html(data).slideDown();//Give error on failure
  10. setTimeout("$('#send_error').slideUp();",4000)
  11. }
  12. }
  13.  
  14. function receive(){//Recieve new messages
  15. $.getJSON('receive.php','p='+prev+'&t='+topic,processReceive);//Request receive.php (p=the previous message user received)
  16. }
  17.  
  18. function processReceive(data){//When receive.php returns
  19. if(data){//If there were new messages found
  20. $.each(data,function(message,messageInfo){
  21. //Basically just adds the messages to the page and set prev as previous message
  22. });
  23. }
  24. setTimeout("receive()",100);//receive() again in 0.1 seconds
  25. }

send.php:
php Syntax (Toggle Plain Text)
  1. $message=trim($_POST[m]);//Get message posted
  2. $topic=$_POST[t];//Get topic posted
  3. if($message&&$topic){
  4. $message=htmlspecialchars($message);//Remove HTML
  5. include "../inc/global.php";//This file connects to database (below)
  6. $sender=$_SESSION[username];
  7. if($sender){//If user is logged in
  8. $time=time();
  9. mysql_query("INSERT INTO messages(topic,sender,message,time) VALUES('$topic','$sender','$message','$time')")or die(mysql_error());//Insert message into database
  10. echo 1;//Success
  11. } else{
  12. echo 'error';
  13. }
  14. } else{
  15. echo 'error';
  16. }

receive.php:
php Syntax (Toggle Plain Text)
  1. include "../inc/global.php";//Connects to database (shown below)
  2. $prev=$_GET[k];//Get ID of previous message received
  3. $topic=$_GET[t];//Get topic
  4.  
  5. if($prev&&$topic){
  6. for($x=0;$x<10&&$newMessagesQuery=mysql_query("SELECT * FROM messages WHERE id>'$prev' AND topic='$topic'");$x++){//Loop no more than 10 times, each time setting $newMessagesQuery as a query for new messages
  7. if(mysql_num_rows($newMessagesQuery)){//If message(s) were found
  8. $nMs=array();//New Messages
  9. while($message=mysql_fetch_assoc($newMessagesQuery)){//Write JSON for each message
  10. $nMs['m'.$message['id']]['id']=$message['id'];
  11. $nMs['m'.$message['id']]['sender']=$message['sender'];
  12. $nMs['m'.$message['id']]['message']=$message['message'];
  13. $nMs['m'.$message['id']]['time']=$message['time'];
  14. }
  15. echo json_encode($nMs);//Output JSON
  16. break;//Stop loop
  17. } else{
  18. sleep(1);//If no new messages were found then sleep for 1 second and check again...
  19. }
  20. }
  21. if($x==10){
  22. echo 0;
  23. }
  24. } else{
  25. echo 0;
  26. }

global.php:
php Syntax (Toggle Plain Text)
  1. $connection=mysql_pconnect("mywebhost.com","avario","******") or die("Could not connect to server.");//Connect to database
  2. mysql_select_db("instantmessenger",$connection) or die("Could not locate database");
  3. session_start();//Start Session

Please note that I have excluded a lot of code that I don't think is important and many names I use in the code above are not really the names I use.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 8th, 2010
0
Re: Live Messaging
I think the problem is server side. It would seem that I can't request a page from my host until the previous page has returned. I can't load another page on the site (which is unrelated) while I am requesting, I have to wait for the request to return.

Is there a setting I could change that would allow me to request 2 pages simultaneously without having to wait for the first one to return?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: help need to update userlist...using ajax/jquery/javascript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: jQuery Select From x To y Possible?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC