Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
ajax interactions

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX general discussion
View previous topic :: View next topic  
Author Message
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Thu Oct 05, 2006 12:10 pm    Post subject: ajax interactions Reply with quote

I am coding a chat software using PHP+MySQL+Ajax with prototype

I have a function that update the chat information each 1 second by setTimeOut (3 Ajax interactions in one functon that refresh users & rooms and texts)
and I have some buttons that they do some other ajax interactions

But I have a problem when I upload it in Internet and chek it with low speed :

always I have failed() function it means my ajax conection Failured see a sample :
Code:

  var myAjax = new Ajax.Request( 'Chat.php', { method: 'post', parameters: pars , onSuccess: show_users , onFailure:failed });


I have this problem only in internet not localhost I think it is for responsetime and maybe setTimeout is not good for this because mabe I have some Ajax connections in one time ! (I guess, I dont know problem yet)

who can help me ?
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Thu Oct 05, 2006 9:58 pm    Post subject: Reply with quote

Every second? That sounds really fast, in fact too fast for high latency networks such as the internet. I'd try to let the connection open until a new event happens. something like
Code:

<?php
while(eventqueue.empty)
  usleep(500);
}
// Now we have an event to send
// send event
?>

The client side waits until it gets the whole answer, visualizes it, and fires the next request.
This way you reduce the serverload (lesser requests) always have only one request active until you have a new request, and some other minor advantages (also it will solve your problem...)
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Fri Oct 06, 2006 12:57 am    Post subject: Reply with quote

thank you for helping

It means I should add this code in first of my PHP codes ?
Code:

<?php
while(eventqueue.empty)
  usleep(500);
}
//my PHP codes
?>

is eventqueue.empty constant ? what is the meaning of this ?
I have this code in my javascript code :
Code:

 setTimeOut(update(),2000);

And this is my update function
Code:

function update()
{
///chek if div is visible update it
  var pars = 'do=load_text&last='+last;
  var myAjax = new Ajax.Request( ' chat.php', { method: 'post', parameters: pars , onSuccess: load_text , onFailure:failed });
  var pars = 'do=load_users';
  var myAjax = new Ajax.Request( ' chat.php', { method: 'post', parameters: pars , onSuccess: show_users , onFailure:failed });
  var pars = 'do=load_rooms';
  var myAjax = new Ajax.Request( ' chat.php', { method: 'post', parameters: pars , onSuccess: show_rooms , onFailure:failed });
}


What should I have change in my client side code?

I am sorry my english is poor !
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Fri Oct 06, 2006 12:08 pm    Post subject: Reply with quote

Let's say that the eventqueue is some sort of event manager that simply queue up the events that happen on your server (chat messages, chat enter, chat exit, ...) and that empty tells you is the queue is empty or not. If no events happen (no one chats, no one exits/enters, ...) the server does not respond immediately to the request but waits for something to happen, as soon as something happens the server writes out the events and sends the reponse. The client receives the events, renders them (in your chat application) and then immediately connects back to the server awaiting for new events.
Not sure if this explains very well, just ask if you need more details ^^
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Fri Oct 06, 2006 8:35 pm    Post subject: Reply with quote

can you help me with a sample,
I think I should have it on javascript but you had in PHP
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Fri Oct 06, 2006 10:33 pm    Post subject: Reply with quote

Ok basically the idea is that you have many requests that were fired and got back no new information (nothing happened in between requests) so you have many, many open connections running parallely (because not always the full roundtrip with request, answer, ... will take less than 1 second, as you told us it isn't working on the internet and the latency is probably the problem) and many browsers have a limit of 2 concurrent connections per server, thus every other requests fails.
Now why have so many connections when you do not get any new information? The solution is holding the request back until there _is_ something new to report ^^
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Sat Oct 07, 2006 6:19 am    Post subject: Reply with quote

Code:

The solution is holding the request back until there _is_ something new to report


But how ?
when I make a connection with prototype it send request and get the awnser (maybe its empty)

Should I chek on server side usleep until the awnser (for example new message in chat) exist ?

I used setTimeOut and run refresh in a loop with this chages that you say if I hold request back the next request send and maybe many request will hold on server !
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Sat Oct 07, 2006 7:46 am    Post subject: Reply with quote

What you do is wait on the server side, until there is something to send. usleep waits for a fixed amount of time, after which you'll have to let's say, check your database if something happened, if nothing happened you keep on waiting, otherwise you send the response and close the connection. The Client receives the connection, renders it and immediately opens a new request. No more need for settimeout on the client side as the server keeps control of the connections and we don't want any concurrent connections.
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Sat Oct 07, 2006 3:57 pm    Post subject: Reply with quote

I have problem yet Sad

Can it make timeout error on server side ?
When php code is usleep the Ajax connection is open and waiting for awnser is it correct ?

Can you show me with a little sample ,please ?
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Sat Oct 07, 2006 8:50 pm    Post subject: Reply with quote

Ok so this is going to be a really quick sample, 'cause I have to go out with a friend of mine right now ^^
Code:

<?php
while(true){
  $events = mysql_fetch_all(mysql_query("SELECT * FROM events WHERE tme > lastupdate ORDER BY tme ASC;")) or die("Error");
  if(empty($events))
    usleep("1000"); // sleep for a second, wait for new events.
  else
    break; // we have some events to send
}
// send the $events array as response.
?>

On the client side you just do something like this:
Code:

var myAjax = new Ajax.Request( 'Chat.php', { method: 'post', parameters: pars , onSuccess: show_users , onFailure:failed });

As the server keeps the connection open until we have something to send. Notice that at the end of the show_users function you should immediately resend the function, as you did with the settimeout statement:
Code:

function show_users(e){
  // do some stuff to render the response
  var myAjax = new Ajax.Request( 'Chat.php', { method: 'post', parameters: pars , onSuccess: show_users , onFailure:failed }); // recall the function again.
}

This way there always is a single connection open to the server through which the data is sent.

Ok got to go, hope it helps.
P.S. non of the code has been tested!
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Sun Oct 08, 2006 6:29 pm    Post subject: Reply with quote

Thank you I undrestand but only one part :

Code:

Notice that at the end of the show_users function you should immediately resend the function, as you did with the settimeout statement:





I need only in this part I can not undrestand this
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Sun Oct 08, 2006 7:57 pm    Post subject: Reply with quote

Ok let's try again. At the beginning you fire the first update. When the response comes back, you render it and then fire the next update request.
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Sun Oct 08, 2006 8:59 pm    Post subject: Reply with quote

thank you for your helps


I think the problem is in my bad english !
Can you show it in up code ?


////
another question :
Should I add for server side codes (response)
a manager with usleep ? like list of users & list of new posts and ....
Back to top
View user's profile Send private message
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Tue Oct 17, 2006 9:15 am    Post subject: Reply with quote

I moved my codes abput getting text to another file and I add this code first of the file :

Code:

while(true){
       $last_post_time = $_SESSION['last_post_time'];
       if (!$last_post_time)
          $last_post_time=0;
      $events = mysql_fetch_array(mysql_query("SELECT post_time FROM message WHERE post_time > $last_post_time ORDER BY post_time ASC")) ;
        echo mysql_error();
      if(empty($events))
             usleep("2000");
        else{
           $_SESSION['last_post_time'] = $events['post_time'];
          break;
        }
   }



is it correct ?

It make delay for adding messages to database !
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Tue Oct 17, 2006 11:28 am    Post subject: Reply with quote

Looks nice, even if I'd rather keep the last read item on the client than the server.
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Wed Oct 18, 2006 7:39 pm    Post subject: Reply with quote

I add this code but It make delay for inserting messages to database Sad !!!
Back to top
View user's profile Send private message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Wed Oct 18, 2006 8:28 pm    Post subject: Reply with quote

Send your messages using a second XHR. Don't use the same one for both Event fetching and message posting.
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
bakhanbeigi
Fairly Frequent Poster
Fairly Frequent Poster


Joined: 05 Oct 2006
Posts: 11

PostPosted: Thu Oct 19, 2006 1:59 pm    Post subject: Reply with quote

what is the XHR ?
I changed the server side page of getting messages and other workds like sending message and sending message have not usleep
is it correct and enofe for difrent event fetching ?
Back to top
View user's profile Send private message
lav123
New one
New one


Joined: 11 Jun 2008
Posts: 1

PostPosted: Wed Jun 11, 2008 6:54 am    Post subject: Reply with quote

Hi I'm recently started to develop a php-ajax website
As i'm new to the web programming field i'm facing a problem

there is a select control in my php page..every time its working fine in mozilla firefox-4 but in Internet Explorer-6 its not working properly

can anyone helpme to fix this bug

this is my code
------------------------------------------------------------------------------

-------------------------------------------------
1.departments.php
------------------------------------------------

<html>
<head>
<title>My web site</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script src="showdepartments.js">
</script>
<body onLoad="ShowDepartments();">
<div id="listdepartments">Department

<select name="selectdept" id="selectdept" size="1">
<option selected>-----</option>
</select>
<?PHP
echo "Php code for functionality on selected department comes here";
?>
</div>
</body>
</html>


--------------------------------------------
2.showdepartments.js
----------------------------------------------

// JavaScript Document
var xmlHttp;
function ShowDepartments()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="showdepartments.php";
xmlHttp.onreadystatechange=departmentstateChanged ;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function departmentstateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("selectdept").innerHTML=xmlHttp.responseText;

}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}





----------------------------------------
3.showdepartments.php
--------------------------------------



<?PHP
echo "<option>-----</option>";
for($i=0;$i<=5;$i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
















Regards
lav123
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX general discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert