FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


AJAX Chat discussion





alalex
Hello everyone,
I need to build a chat for a site I'm working on, and I think I'll go with an AJAX approach. I just wanted to discuss this with you guys here, to see what would be the best solution, from the server's point of view.

My idea would be to simply store messages in a database, with a maximum of 100 at the same time or so. Then on the client side, have JavaScript send AJAX requests to get new messages every 5 seconds or so, and display them on screen.
Sent messages would simply be stored via AJAX as well...

I am simply afraid that if many users use the chat at the same time the server might be a bit stressed don't you think? Would you do it in a different way?

Thanks in advance,
Alex
jmraker
If I did an ajax chat, I'd keep the "last viewed" timestamp in the session or in the database and only return the new messages. And perhaps have the server control how often the javascript updates.
Stubru Freak
I'm not sure this is possible, but I think you could keep the connection open until there's a new message. Then it would instantly update. The amount of traffic is reduced this way, but there's always an open connection to every chatter, which could be a problem.
alalex
Well I'm not sure you can actually "keep a connection open" with AJAX, the closest you can is set an interval and have it poll the server.

An "open connection" would be if you had a Java based chat, or something of the sort, but I don't want to code a full applet only for this.

I will go with AJAX and php, I think it's the fastest way, I'm almost done now, so I'll be able to start testing the traffic it generates,

Thanks,
Alex
Stubru Freak
alalex wrote:
Well I'm not sure you can actually "keep a connection open" with AJAX, the closest you can is set an interval and have it poll the server.

An "open connection" would be if you had a Java based chat, or something of the sort, but I don't want to code a full applet only for this.

I will go with AJAX and php, I think it's the fastest way, I'm almost done now, so I'll be able to start testing the traffic it generates,

Thanks,
Alex


I think you can, but I'm not sure how.
TomS
alalex wrote:
Well I'm not sure you can actually "keep a connection open" with AJAX


Check out the php-function mysql_pconnect. This way your script only uses one connection instead of many simultaneous connections that open and close all the time.
Stubru Freak
TomS wrote:
alalex wrote:
Well I'm not sure you can actually "keep a connection open" with AJAX


Check out the php-function mysql_pconnect. This way your script only uses one connection instead of many simultaneous connections that open and close all the time.


That's for mysql, not for the connection to the browser.
alalex
Well of course something like this would require a mysql_pconnect

I've been searching around, and I don't think it is possible to have the server send automatically a notification to the client as soon as a new message appears, I think the only way is using an interval for the javascript function,
TomS
Stubru Freak wrote:
TomS wrote:
alalex wrote:
Well I'm not sure you can actually "keep a connection open" with AJAX


Check out the php-function mysql_pconnect. This way your script only uses one connection instead of many simultaneous connections that open and close all the time.


That's for mysql, not for the connection to the browser.


I didn't say, it's for the browser Rolling Eyes
But one persistent connection is surely better than lots of connects and disconnects.

alalex wrote:
Well of course something like this would require a mysql_pconnect

I've been searching around, and I don't think it is possible to have the server send automatically a notification to the client as soon as a new message appears, I think the only way is using an interval for the javascript function,


You can't push content to the client without a request. For a steady connection, you need flash or something similar.

I would recommend www.speeqe.com
It's an opensource Ajax(?) client for Jabber's Multi-User-Conferences
alalex
Yea that's why I was thinking about using a Flash based chat, but I think it will be easier/faster to create an AJAX based one, I'm almost done already and I need this project ready by the end of the week so this will do... I hope Smile

If you have any great idea or something please share!
Stubru Freak
I did some research today and you can actually keep an open connection all the time using just AJAX and PHP. The source for this code is http://us3.php.net/manual/en/function.set-time-limit.php

Code:
<?php

set_time_limit(900);

// Start output buffering
ob_start();

$message = "First test message";
$oldmessage = "bla";

// Keep on repeating this to prevent PHP from stopping the script
while (true)
{
    $timeoutcounter = 0;
    while ($message == $oldmessage)
    {
        // If 10 seconds elapsed, send a dot (or any other character)
        if ($timeoutcounter == 10)
        {
            echo ".";
            flush();
            ob_flush();
            $timeoutcounter = 0;
        }
        // Timeout executing
        sleep(1);
        // Check for a new message
        $message = file_get_contents("chatdata.txt");
        $timeoutcounter++;
    }

    // Keep the old message in mind
    $oldmessage = $message;

    // And send the message to the user
    echo $message;

    // Now, clear the output buffer
    flush();
    ob_flush();
}

?>


Here it gets the chat text from a file instead of a MySQL database, but it can be easily adapted for a database.
The server sometimes sends a dot instead of a message, to keep the connection open. You can just make your AJAX script ignore the dots.

It will keep an open connection for 15 minutes, maybe a little less depending on the client and the server. If you lose your connection, just make a new one.
alalex
That sounds very interesting!

I will set it up to do some benchmarking and see how this affects the server, thanks a lot,

Alex
Related topics
Chaos Magick
Old fight issue, Flash or Html, Macromedia has a tip?
Www.netvibes.com :: Cool Ajax with RSS feeds site!
AJAX tutorials?
Hosting Rules - Scripts
Live Chat.
Make me an Ajax-Chat for 500 Frih$
how can you design a webpage with live chat?
phpBB3 ~~ Board Features Problem
How is everyone today?
Best chat for a website?
PHP-based Chatroom
Looking for the best Flash Chat
long polling concerns and tips?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.