Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:29:58+00:00 2026-06-15T18:29:58+00:00

I am creating a chat program. This chat program has two sides(client and user).

  • 0

I am creating a chat program. This chat program has two sides(client and user). All the data is going into a database(mysql). Currently, the chat works fine. Each side types and i have a listener function that uses ajax to load the database file into the window each second or two.

The problem is, this is eating up too much bandwidth!

I was thinking about terminating the chat after a set duration or I was thinking there is a way to only update when an event happens.

Ideally this would work best in my opinion:

If the user enters new data, then it will be detected on the client side and then it will activate the function to update the chat window only at that time.

Does anything exist like this to listen in ajax/jquery/javascript?

Here is the code I am currently using to listen:

/* set interval of listener */ 

 setInterval(function() {
listen()
}, 2500);

 /* actual listener */

 function listen(){
    /* send listen via post ajax */
    $.post("listenuser.php", {
        chatsession: $('#chatsession').val(),       
/* Do some other things after response and then update the chat window with response content from database window */
    }, function(response){
        $('#loadingchat').hide();
         $('#chatcontent').show();
        $('#messagewindow').show();
        setTimeout("finishAjax('messagewindow', '"+escape(response)+"')", 450);
    });
    return false; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:29:59+00:00Added an answer on June 15, 2026 at 6:29 pm

    There are many ways to do it, but looking at your design I will suggest to use a technique called Comet, basically is a way to get web servers to “send” data to the client without having any need for the client to request it. It is kind of a hack if you read the code, as per my opinion.
    but here goes a example that i found of how to implement this, using a simple text file:

    SERVER

    <?php
    
      $filename  = dirname(__FILE__).'/data.txt';
    
      // store new message in the file
      $msg = isset($_GET['msg']) ? $_GET['msg'] : '';
      if ($msg != '')
      {
        file_put_contents($filename,$msg);
        die();
      }
    
      // infinite loop until the data file is not modified
      $lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
      $currentmodif = filemtime($filename);
      while ($currentmodif <= $lastmodif) // check if the data file has been modified
      {
        usleep(10000); // sleep 10ms to unload the CPU
        clearstatcache();
        $currentmodif = filemtime($filename);
      }
    
      // return a json array
      $response = array();
      $response['msg']       = file_get_contents($filename);
      $response['timestamp'] = $currentmodif;
      echo json_encode($response);
      flush();
    
    ?>
    

    CLIENT:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Comet demo</title>
    
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="prototype.js"></script>
    </head>
    <body>
    
    <div id="content">
    </div>
    
    <p>
    <form action="" method="get" onsubmit="comet.doRequest($('word').value);$('word').value='';return false;">
        <input type="text" name="word" id="word" value="" />
        <input type="submit" name="submit" value="Send" />
    </form>
    </p>
    
    <script type="text/javascript">
        var Comet = Class.create();
        Comet.prototype = {
    
            timestamp: 0,
            url: './backend.php',
            noerror: true,
    
            initialize: function() { },
    
            connect: function()
            {
                this.ajax = new Ajax.Request(this.url, {
                    method: 'get',
                    parameters: { 'timestamp' : this.timestamp },
                    onSuccess: function(transport) {
                        // handle the server response
                        var response = transport.responseText.evalJSON();
                        this.comet.timestamp = response['timestamp'];
                        this.comet.handleResponse(response);
                        this.comet.noerror = true;
                    },
                    onComplete: function(transport) {
                        // send a new ajax request when this request is finished
                        if (!this.comet.noerror)
                        // if a connection problem occurs, try to reconnect each 5 seconds
                            setTimeout(function(){ comet.connect() }, 5000);
                        else
                            this.comet.connect();
                        this.comet.noerror = false;
                    }
                });
                this.ajax.comet = this;
            },
    
            disconnect: function()
            {
            },
    
            handleResponse: function(response)
            {
                $('content').innerHTML += '<div>' + response['msg'] + '</div>';
            },
    
            doRequest: function(request)
            {
                new Ajax.Request(this.url, {
                    method: 'get',
                    parameters: { 'msg' : request
                    });
            }
        }
        var comet = new Comet();
        comet.connect();
    </script>
    
    </body>
    </html>
    

    I hope it helps.

    heres a URL with more examples and some documentation (I got the example from here):
    http://www.zeitoun.net/articles/comet_and_php/start

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an xmpp chat client for android. Everything related to asmack has
I'm creating a chat client which uses a database to check the status of
I'm creating a chat program, similar to IRC. With my client though, I have
I'm creating a chat system but the user table is from a seperate database.
I am creating a server chat program in java using DatagramSocket and datagramPacket im
For a while, I've been interested in creating a proof-of-concept chat program using C++.
I am creating a chat application. In this chat application, I have to use
I am creating a network chat client in C# as a side project. In
I'm creating a chat widget that will be dropped into CommunityServer. The widget works
Let's say you're creating a database to store messages for a chat room application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.