Jump to content

Events in PHP


sellfisch

Recommended Posts

Hi

 

a very common problem of the http protocol is that the communications is allways initiated by the client. In my opinion the server has no real chance to push additional data after he has send its answer.

As i can see from different ajax applications like Meebo or other ajax-chats. They wait with the serveranswer until something happens.

 

But how to realize that in PHP without stressing the server? First i tried to handle events via a loop:

for(;{
    $result=mysql_query(Something new?);
    sleep(1);
    if(something new?) break;
}
echo "something new!";

But this way has to great disadvantages:

  • Every user creates one sql-query per secound
  • The resonse time takes up to one secound

 

To solve the two problems i found another way to realize that:

<?
session_start();
set_time_limit(0);
$myport=getMyPort(10000,15000); //selfwritten function which provides a free port in a given port range
if(!$socket=socket_create_listen($myport)){
echo "Socketfehler";
exit(1);
}
$new_socket=socket_accept($socket);
$buffer=socket_read($new_socket,1024);
echo "something new ($buffer)";
socket_close($socket);
socket_close($new_socket);

This code creates a listening socket and waits for a request. This is a very nice behavior because the script is doing nothing until something sends a package to its port.

Maybe you can provide another script which is able to release the event by doing:

$socket=socket_create(AF_INET, SOCK_STREAM, 0))
socket_connect($socket, "localhost", $portOfTargetUser))
socket_write($socket,"stop waiting!");
socket_close($socket);

 

But my questions is, can i realise that in a different way? Using (Networkports) seems to be very unprofessional and unsecure to me.

Is it possible to get an event, when a file is changed? Or it is possible to create global events?

 

 

Link to comment
Share on other sites

For example you have on a chat, every user is waiting for a new message. But to ask the database every secound for a new message, will slow down your system. So it would be nice to send a request to the server and it waits with the answer until there is a new message in the system (like meebo and some other servicers do).

Link to comment
Share on other sites

How can i edit my posts? what ever. I made a little picture about my problem. Hope its helps:

somethingnew.png

 

Javascript can only work on the client side. So it doesn't help to detect new messages.

I wan't to improve the "waiting-state" (see picture) on the serverside. How can the script know that there is a new message (in the db or where ever) without looking it up every secound (or more often).

 

One solution i found goes the following way:

  • A orders get.php via javascript(ajax request)
  • get.php creates a socket for A
  • get.php stores A's port to the database
  • get.php of A starts listening on his port
  • B sends a message to the server
  • The server looks up the port of user A
  • B sends a message to Port of A
  • A's get.php stops listening and send the message back
  • Javascript gets the message and send a new get-request

 

But ther sould be a better way than using sockets. Because i think its a security risk to open up networkports for every user of your webapplication.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.