www.webdeveloper.com
Results 1 to 4 of 4

Thread: Chat Room with private chat

  1. #1
    Join Date
    Feb 2013
    Posts
    4

    Red face Chat Room with private chat

    I have a question for you all... I am creating a Chat Room that has the possibility of creating private rooms. I have done this using Javascript, PHP, and a MySQL database. All works and functions fine but from time to time I get multiples... more defined explanation follows...

    The application consists of 3 "windows" / "panels":
    the private chat users
    the public chat users
    the chat history (switchable between private and public chats)

    Pretty self explanatory, right?

    I have the script initialize setting default/global variables and setting up three setInterval() to update those three "windows" / "panels"... like I said it all works fine except every once in a while it is like the responseText from one of the intervals hangs up and then the interval executes 3 - 5 more times before receiving the first... so, if within this time someone has added a chat message it gets output 4 - 6 times instead of once. Or a user that joins during one of these hiccups gets added to the list 4 - 6 times... etc... you get the picture.

    My question is if I am using the best solution... or if I should maybe consider another method for updating this content? I added a check on the private chat users list to make sure if they are already listed not to added them again and so it doesn't but the scripts are still executing and retrieving the content regardless of whether or not it is going to use it... and in the long run I would rather not be transporting data that is not going to be used... Basically, what I am doing is on a query I am sending the id of the last retrieved, and the PHP script only fetches and returns that which is new. So, in this case... during the "hiccup" the new is being retrieved and returned multiple times...

    I have been looking into Comet, BOSH, XMPP but I am uncertain as to whether or not this will be beneficial in the long run... as to whether it would make things easier on the server or create a larger strain than necessary. I mean, if I have to work harder to just optimize my code to make sure there are no cracks for things to slip through then I would rather do that than to take, and use, an application that is pre-built, and therefore simplifies things for me, but makes things more strenuous on the server.

  2. #2
    Join Date
    Mar 2009
    Location
    Wherever i lay my hat
    Posts
    180
    You could use setTimeout() instead and send another request only after the previous succesfully returns. And maybe you could add some IDs for the chat messages as well and check, whether they exist already in the document, before you output them.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    var used={}; //top of file
    
    //in ajax handler:
    if(!used[x.responseText]){
      used[x.responseText]=true;
      updateContent(x.responseText)
    }
    works best if you append a date to the message when sending it, that way "ok" alone can be send twice...

  4. #4
    Join Date
    Feb 2013
    Posts
    4
    Thanks you both, I appreciate the input.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles