We help IT Professionals succeed at work.

PHP Chat

Konstantin Krolikov
Konstantin Krolikov used Ask the Experts™
on
hello experts,
i created apllication and i want to add chat, so i created a simple chat room, working on a file.
so everything working.

i check over the chrome and i saw that the ajax response is around the 400B.
so i check the chat in facebook and i saw that they use 1 request witch called "pull" and she is on Pending state until something happend(i have new message).

so how do i do that, how do i save many sends to the server.

my idea was that facebook simply preform some loop and until there is no new infomation, sleep for 1 sec, so the server do all the work slowly and the save the data, but i think there is better solution.
and way on facebook chat the request is in Pending and mine just get the result.

by the way, chrome get the type as undefined in facebook, and in my chat it is text/html.
(same deal for gmail, almost exacly the same..)

basically what im saying is that my chat do a lot of request to the server while any other normal chat (gmail, facebook) have a so much litile request...

thanks ahead experts!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:

Author

Commented:
well, i think i have no choice but to use php and ajax, however if i send the request to the server and until there is no new information i won't return a thing, with the sleep function.

this way is better? i think send much less request's to the server and yet, give me nice performance..
leakim971Multitechnician
Top Expert 2014

Commented:
>i think send much less request's to the server and yet, give me nice performance..

ajax push vs ajax pull, the common way
The connexion stay open as long as possible so you reduce number of server request dramatically.

http://www.ape-project.org/ajax-push.html

Author

Commented:
i didn't undestand, i'll have to run additional server?
leakim971Multitechnician
Top Expert 2014

Commented:

Author

Commented:
im sorry, but as far as i checked this APE doesn't support major browsers, in face, it doesn't work on any browser! anly at chrome.. i need it to be on mobile and everything.
i need full support as jquery and ajax have..
can i do this simply with sleep function at php or there is better way?
leakim971Multitechnician
Top Expert 2014

Commented:
>im sorry, but as far as i checked this APE doesn't support major browsers, in face, it doesn't work on any browser! anly at chrome

that's wrong, ajax push is the way to go,
you want to limt number of request from the client, you did not say you want to limit the number of responses (any tricks with sleep & co on the server side with or without php)

Author

Commented:
yeah, you right, however, in order to down the number of requests and response from/to the server i'll use the sleep function, and i'll perform the ajax function only if there is no other previous request.
in that way, i'll have to browser send request and if there is a new information, he will process it, and only when finish he will send new request to server(only after finsihed the first one).

in addition, it is very acurate time, because until there is no new result, the server simply drag the time. i get better performance and it's pretty much do what i want it.
but if you say that this is wrong way, then i need diffrarent way.

so how shold i do chat that will work in all internet devices, desktop and mobile.
by the way, i saw that facebook and gmail do pretty much the same, one request, witch take a lot of time, unless there is something happend..
leakim971Multitechnician
Top Expert 2014

Commented:
sleep will not reduce number of request from client

Someone is sleeping on the sofa, anybody can continue to ring its bell, isn't it?

Author

Commented:
hh yeah, but if i make the server sleep then the client code will not send request until he gets the answer to the previous one.
so you send request, if there is no new information, then you simply wait for max 30 seconds. and only after you get some response, client send another request, and then same process..
are you sure it won't reduce the nubmer of request form the server..

thanks alot :)
leakim971Multitechnician
Top Expert 2014

Commented:
hh yeah, but if i make the server sleep then the client code will not send request until he gets the answer to the previous one.

not sure what we're talking about but if this is a timer (most of time you send request with a timer) or with a click I don't see anything stopping the client to send a new request.

pendant request don't stop new request... or perhaps you set some flag and in this case I see some problem to handle error and pendant request

everone use ajax but you right you can try to find a better way that's the way to think... when you've time.

Author

Commented:
well i thought to use ajax, and only when there is a response from the server, send new request.
so there is no client timer, client simply send request, wait for respose, when he got the response, process and then simply send another request, and that "loop" gone until he out of the page, so this way he get new response only if there is new information(new message) otherwise, client simply wait... and wait... and wait....
leakim971Multitechnician
Top Expert 2014

Commented:
no ajax push say
hello browser are listeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeening
end of call
hello browser are listeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeening
end of call

Open in new window


ajax pull say :
hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?hello any news? hello any news?

Open in new window

Author

Commented:
ya i know, so what im saying is ok? because that what i ment.

and as i said, i checked over my webrowser and only chrome work with ajax pull engine, so is there any other way.. ?
leakim971Multitechnician
Top Expert 2014

Commented:
>only chrome work with ajax pull engine

where di you see that? that's wrong !

Author

Commented:
i checked it over my computer, i have firefox, ie, safari and chrome. i search for some demoes and only in chrome the demo worked, in the other webrowser the game or the chat room doesn't event presented..
leakim971Multitechnician
Top Expert 2014

Commented:
not sure where it fail but it's javascript, only javascript

Author

Commented:
can you give me a link with a simple demo or tutorial with that ajax pull, because i just remember that in the facebook chat, there is some ajax action called pull. nothing every responsed!!!
how do i do ajax pull??

thanks alot for everything..
leakim971Multitechnician
Top Expert 2014

Commented:

Author

Commented:
well, it doesn't help me, i already read and perform all the links in google... hhh..
but none of them show me how to do ajax pull as you said..
leakim971Multitechnician
Top Expert 2014

Commented:
ajax pull, is the common ajax no more

Author

Commented:
so can you give me a simple script??
take a look at this code.
$.ajax({ url: "some.php", type: "POST", dataType: "JSON", data: {"function" : "update"}, success: function(response){ ...some process...     and send new ajax request..}

Open in new window


is that what you mean... i can't understand you, you told me to do ajax pull, so how?
and is it involved some php code?
leakim971Multitechnician
Top Expert 2014

Commented:
yes you line of code is an ajax pull

$.ajax({
     url: "any_new_message_from_someone.php", 
     type: "POST", 
     dataType: "JSON"
     data: {"function" : "update"},
     success: function(response){ 
            if(response.theresmessage) {
                 $("#container").append(response.newmessages+"<br />");
            }
      }
})

Open in new window

Author

Commented:
so that is what i did, but if i set an interval then every second i have new request from each user.
so I ask you, do you think that i'll do it without intrverl, only function that call herself when finished. so i have function with ajax code inside and only if there is no request to the server, send request.
and in the server-side i make the code spleep for  a second, and after that, check if there is new message to the user, if there is then stop the while loop, and print the new message, otherwise, sleep for 1 second and then again.. checking, processing, new message->print to client, else run the loop again. (max 30 second with no response)

so is that way is ok, because i want do reduce the amount of requests that send to the server.
and the sleep function might to that...
what do you think?

example for the code:
client:
var instanse = false; //Default
function chatUpdate() {
	if (!instanse) {
		$.ajax({
			type: "POST",
			url: "ajax/pull.php",
			data: {"function" : "update"},
			dataType: "JOSN",
			success: function(data){
				if (data.newMessage)
					//Perform code..
					...
				instanse = false;
				chatUpdate();
			}
		});
	}
}

Open in new window

something like that...

server side:
<?php 
$sleep = 30;
$log["newMessage"] = false;
$query = mysql_query("SELECT QUERY") // Select Statment;
$result = mysql_fetch_row($query);
while (!$result && $sleep) {
	$sleep--;
	sleep(1);
}
if ($result) {
	$log["newMessage"] = true;
	$log["data"] = $result; //print the information
}
echo json_encode($log);
?>

Open in new window


again, this is the idea, my question is whether i should do it this way, or this is completly worng.
leakim971Multitechnician
Top Expert 2014

Commented:
Again, using sleep on the server will NOT reduce the number of request from the client.

when you sleep, world continue to run. It's not because you want to ask your server to do no answer (sleep) than client can't send him new request.

additionnaly, a request is a process on the server, if your browser send two requests, the first one, you initiate a sleep, the second one, you initiate a second sleep because it's not the same process

you can find way to figure if you don't want to answer (sleeping flag, cookies, session, data and so on) but you will not reduce the number of request

if you ask the server if there's news to load data or not you make two requests instead one

Author

Commented:
ahhh, so how do i make the client wait for answer from the server, while the server checking if there is new information for the client, and do only one request..
because my solution is to reduce the number of request that transfer from the browser to the server, but the server still have the same(if not more) requests.

so i understand what you mean, but how do i do this..?!!?!

thanks a lot for your help!
leakim971Multitechnician
Top Expert 2014

Commented:
ajax pull open a connexion and close it when you go answer

ajax push let the connexion open on the client side, the server send new data if there's new data; each time the connexion is closed, a new one start so no need to use interval
you reduce dramatically the number of request

I sent you a link with a demo, try it, and use the code if you like it.

Author

Commented:
ok, thanks a lot for every thing, and just so i know that i got what you said right..
the ajax pull waiting for response, but how come the server doesn't response.?
how do you make the server wait with the response and still check..??
can wait to see the code,

thanks alot!!! :)
leakim971Multitechnician
Top Expert 2014

Commented:
ajax pull MUST use a timer
after a delay, you got a timeout

a timeout do NOT stop the timer to run
if you timer delay is 5 seconds and you ajax timerout is 7, you do a call each 5 seconds
if you use a flag to block the next call you start rethinking the ajax PUSH logic...

You said : << i already read and perform all the links in google >>
Each link you read is probably a good example of ajax pull

Author

Commented:
but i already managed to do ajax with timeout, that each 5 second (for saying) call new request, but there is 2 main problems with that.
1. it have a huge delay.
2. it DOESN'T reduce any server request, it just make them less offten..

please give me some easy code about that pull ajax because i have no idea what you mean, i can't understand how can i have delay of 5 seconds in chat application...

thanks again..
leakim971Multitechnician
Top Expert 2014

Commented:
<< to reduce number of request you should use ajax push >> 
Let me know what you don' understand in this sentence.
If you need ajax push chat example see the link I provide to you : Posted on 2012-04-18 at 08:21:38ID: 37861459

If you still want to use ajax pull with you OWN code :
You need to create a new post to find why you have huge delay, provide your code to help EE member to help you in debugging your application or provide a link to see it
if you want example of ajax pull, again : by: leakim971Posted on 2012-04-18 at 14:56:34ID: 37863375
greetings Puzatiy, , I did some web site Chat, in a couple of different ways, , and I had to do alot of research for my second attempt. Since you are asking about the ajax PUSH technology, I will give you what my view is on this, I have read and did code for several ajax Chat, and tried a couple of the PHP chat class availible.  If you look up about PHP chat (ajax), many will say that the PHP server responce configuration is NOT a good setup compared to some JAVA, and other light weight initiation web server options. Most say that PHP can NOT support more than about 400 chat clients at one time using the Pull ajax.  Many also say that PHP can not effectively support PUSH ajax tech, because PHP was setup especially to get a HTTP request, process that request and as quickly as posible send back the HTTP response fast, , , so sitting in a wait state (required for PUSH ajax) was not what PHP was designed for.

You mention FaceBook Chat, , facebook tried to do chat with PHP, and that did NOT work for them, they had to switch to a fast light-weight specialty server response platform that only does their Chat.

I tried to do a Chat that was suppose to be a sort of Hybrid combination of Push and Pull ajax using a IFrame (my server had a PHP timeout for a PHP script of 30 seconds). So The php script would run for 25 seconds, checking for a new chat post every second, and send back a responce to ajax if new post, and if no new then would send a "renew" response to end the PHP script and start a new wait state. But it was BIG problems for me, and never worked. And besides in PHP you can not set up another PHP script to wake up a sleeping (or waiting) client script that is NOT in that process, and I believe that this is required for an effective PUSH chat tech. Each PHP script will have to query something in it's own process every 2 seconds or so, which is almost the same server work as an ajax HTTP responce ever 2 seconds.

Just a suggestion, if you are having more that say 25 or 30 chat clients at the same time, DO NOT USE A MYSQL database to store and retrieve the chat posts, If it is like my setup your DataBase will be swamped with unnecessary requests that will slow down your entire site that uses the Database.I switched to a File based chat data storage, which speeded up everything, including the chat.

If you are not going to have more than 200 chat clients at a time, you might consider using the ajax PULL with a javascript request timing of about 2.5 seconds. But PHP is not really made for chat, at least that my opinion.