I am trying to setup socket.io chat server. I have gotten it up to send and receive message from server and client. I was interested in showing users if some one is typing. I know i can emit when some is typing from client to server, I can also broadcast it to other users. But will it be efficient to do that on every keyup? How do i handle such situation? Below is my code for now:
$("#textbox").keyup(function (e) {
if (e.keyCode == 13) {
socket.emit('send', {nickname: $('#nickname').val() , msg: $("#textbox").val()});
$('#textbox').val(''); }
else{
socket.emit('is typing', {nickname: $('#nickname').val()});
}
});
and on server side:
socket.on('is typing', function(data){
socket.broadcast.emit('typing', {nickname: data.nickname});
});
You can use timeouts to send "started typing" and "stopped typing" messages, like so:
It's rough, but you should get the idea.
Just thought I'd add my variation on the very helpful answer given by abject_error. This allows for the status of typing as well as stopped typing but message was entered. I'm not using socket.io but you get the idea.
Just adding my code here following this tutorial: https://www.youtube.com/watch?v=FvArk8-qgCk&index=5&list=PL4cUxeGkcC9i4V-_ZVwLmOusj8YAUhj_9
Note how it doesn't need the typing boolean variable