View Full Version : AJAX How to display 'User is Typing....' on chat script?
clowes
12-15-2009, 04:06 PM
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.
jscheuer1
12-15-2009, 04:37 PM
I'm assuming you have the server side of this worked out. If so, this should work:
document.getElementById("state").innerHTML = document.chatform.guestname.value + " is Writing....";
clowes
12-15-2009, 05:21 PM
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
djr33
12-15-2009, 07:32 PM
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.
clowes
12-15-2009, 11:35 PM
Thanks for the detailed response. Based on that I will look into a way of doing it. Cheers
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.