Skip to content Skip to sidebar Skip to footer

Php Reload Chat-box?

I have made a chat box like thing in PHP. The only problem is that it doesn't automatically reload. Is there a way to make it reload in PHP, or would I have to move everything arou

Solution 1:

You could use a meta refresh tag, that could be generated by PHP, but its not really a PHP-specific feature.

<metahttp-equiv="refresh"content="600">

This will refresh the content after 600 seconds. You could throttle that time more accurately based on your applications demands.

Ajax is probably the preferred method for checking user inactivity. Here is a similar post on checking user activity - How can I detect with JavaScript/jQuery if the user is currently active on the page?

UPDATE

As for the second question of how to handle exit of a user in Firefox, its like you said that onunload can be inconsistent. This is where the use of ajax shines again. If your app stops receiving ajax updates from the client, you can do some sort of cleanup to mark them as inactive.

We had a similar situation in an ASP.Net MVC app. We ended up using Application Variables to store recent activity and state of users. A php example can be found here.

Hope this helps!

Solution 2:

You could add some JavaScript to automatically refresh the entire page, but using AJAX would be far better for the end user.

Solution 3:

the way Facebook does it, is that client "Alice" has a pending request for new messages open for about 1 minute. if another client "Bob" writes Alice a message, the server can complete Alice's pending request. Alice then instantly gets the new message and opens a new pending request.

you will have to timeout pending requests after about a minute or the browser does it. different browsers have different timeout settings, so just use something small. after a timeout you open a new pending request to not miss anything.

i got some code to play around with: listen.php (client calls this to create pending request, waiting for message nr $mnr of the event $eid.

define('WAIT_MAX', 55); //wait max 55 sec
define('WAIT_INT', 1);  //wait1 sec per call

$start = time();
while ($start + WAIT_MAX > time())
{
    // check if an event occured
    $res = mysql_query('SELECT * FROM event WHERE `eid`="'.$eid.'" AND `mnr`="'.$mnr.'"');

    if (mysql_num_rows($res) > 0)
    {
        // event occured
        $row = mysql_fetch_assoc($res);
        $msg = $row['msg'];

        echo "<event eid=\"$eid\" mnr=\"$mnr\" msg=\"$msg\" />\n";
        die();
        //die('event occured: '.$msg."<br>\nNext mnr=".($mnr+1));
        //break;
    }
    else
    {
        //no event occured
        mysql_free_result($res);
        sleep(WAIT_INT);
    }
}
die(WAIT_MAX . ' seconds passed and no event occured.');

Bob sends its message to yell.php providing $msg

if (isset($_GET['msg']))
    $msg = $_GET['msg'];
else$msg = 'no message given, just firing the event.';

mysql_query('INSERT INTO `event` (`eid`, `mnr`, `msg`) VALUES ("'.$eid.'", "'.$mnr.'", "'.$msg.'")');

some init.php to make it work:

$eid = -1;
$mnr = -1;

if (isset($_GET['eid']) && isset($_GET['mnr']))
{
    $eid = max(0, (int)$_GET['eid']);
    $mnr = max(0, (int)$_GET['mnr']);
}
elseif (isset($_GET['eid']))
{
    $eid = max(0, (int)$_GET['eid']);
    $mnr = 1;
}
elsedie('no eid given');

use and modify this code as you like.

Post a Comment for "Php Reload Chat-box?"