I'm playing around with Coldfusion 10's websockets and made a simple chat for testing. I've seen several chats where they have the "user is typing..." text that shows up when the other user is typing. Does anyone know how to implement this efficiently?
Coldfusion Websockets "User is typing..." Functionality
1.3k Views Asked by Guest At
2
There are 2 best solutions below
0
On
Inside the Comments is a question on how to filter for one on one messaging. Here is some sample code for achieving this.
Instead of using the subscribeTo attribute, use the js function to subscribe the user and pass in some header values. These headers can then be used as filters on the publish call using selector
Example:
<cfwebsocket name="ChatSocket" onOpen="openHandler" onMessage="msgHandler" onError="errHandler">
<script>
function openHandler(){
//Subscribe to the channel, pass in headers for filtering later
ChatSocket.subscribe('chatChannel',{name: '#Session.Auth.FirstName#', UserID: '#Session.Auth.UserID#', AccountID: '#Session.Auth.AccountID#' });
}
function publish(txt, userID){
var msg = {
AccountID: "#Session.Auth.AccountID#",
publisher: '#Session.Auth.UserID#',
id: userID,
message: converthtml(txt)
};
//When including headers, the "selector" is where you will filter who it goes to.
var headers = {
AccountID: "#Session.Auth.AccountID#",
publisher: '#Session.Auth.UserID#',
id: userID,
selector: "UserID eq '"+userID+"' and AccountID eq '#Session.Auth.AccountID#'"
};
ChatSocket.publish('chatChannel',msg, headers);
}
function msgHandler(message){
console.log(message);
}
function errHandler(err){
console.log(err);
}
</script>
Create one more channel 'notification' and publish into it 'User is typing' when user does a 'keyDown' and 'emptystring' when 'keyUp'.
Subscribe to this channel along with you chat. the target of this channel on the receiver side shall be a whose inner html can be populated with 'User is Typing' message.
PseudoCode:
Another enhancement will be to have a div already with the text 'user is typing' and your channel broadcasts the text as 'show' and 'noshow'. This is basically the class name given to the to show and hide it. Less traffic.
Approach 2: Using the same channel
One can always trick this code by typing the message as '@userTyping@-yes' or '@userTyping@-no'. But as i said, this is just a POC. Also, for the timeout you have mentioned, the keyUp would take care of it anyway. But you can also call the notifyHandler() by a setTimeout() as shown above.