Results 1 to 5 of 5

Thread: AJAX How to display 'User is Typing....' on chat script?

  1. #1
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default AJAX How to display 'User is Typing....' on chat script?

    Hello.

    Basically I am making a chat script.

    At the moment I have an onkeypress event which calls a Javascript function, and changes the Inner HTML of a div to say "Writing....".

    Problem is this only works for the user actually writing. All the other users in the chat have no idea that you are writing. How would I display "User X is writing... " on the screens of the other users?

    Small point (I am new to this). I have:

    document.getElementById("state").innerHTML = "Writing....";

    The username is stored in +document.chatform.guestname.value+

    What is the correct way of outputting "Name, Writing...." because

    document.getElementById("state").innerHTML = "+document.chatform.guestname.value+ Writing....";

    returns errors.


    Thanks for all the help.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm assuming you have the server side of this worked out. If so, this should work:

    Code:
    document.getElementById("state").innerHTML = document.chatform.guestname.value + " is Writing....";
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Many Thanks John.

    That is perfect for the latter half of my question.

    Basically id User A types a message, it displays on his screen "User A is Writing..."

    My main problem is getting it to display on User Bs screen "User A is writing.."

    How could this be accomplished?
    Thanks

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Is this actually a Javascript question?

    Chat scripts are very difficult to write because of how the web works. Serverside code is only run when the browser loads a new page (or does an ajax request), and clientside code cannot store anything to the server (except via ajax).

    So storing the data is fairly easy: use javascript to trigger a php script that stores the data in the database or other storage location (ajax). But having this new data trigger the remote machines to actually do something is impossible. Instead you need to have a loop operating on all machines connected to the chat and have them LOOKING for feedback.

    In other words, there is no way for a new message to make the other clients display it; instead, all the clients must run a loop that constantly checks for new messages. If it checks once every second, then it will waste a check every second there is no new message AND only update the chat every second. If you change that value (one second) it will make it significantly worse for one of those and you have to decide what works best. Ajax requests more than once a second may not go through (even that is a lot for a slow connection), and any more than a second between messages will feel like a big lag in the chat.

    As for your question about status, assuming you have figured out messages, there are basically two options:
    1. Create a similar method to how the clients check for new messages on the server and now have two ajax loops running, this one now checking for status updates (typing/not typing) for any users they are talking to.
    2. Use the same loop that you do for messages (I don't know if this will work because you didn't post any code or explain the method), and now have two (or more) parts: basically in each one second loop you will get a packet from the server with any information. This is a much more complex method but it will help you add any new features, for example users logging on and logging off.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the detailed response. Based on that I will look into a way of doing it. Cheers

Bookmarks

Posting Permissions

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