Why don't we shift your thinking a little. Put the burden on the client, not the server, for the typing.
Up vote 1 down vote favorite 1 share g+ share fb share tw.
I'm developing a messaging system and I would like to display when a user is typing or not. How I figured I would do it is just have a table in my database called "typing" with the columns for who is typing and who they're typing to and the time. Every time they press a key in the input field I would post to the database (seems like this would slow the site down though).
Then I'd run some javascript that checks if the column in the typing field is older than 5 seconds, if not then I display the message, if it is then I remove it. This seems to be a pretty straight-forward way to do it, but I was wondering if there is a better option. I am uncomfortable with such frequent posts to the database.
So does anyone know a better way this can be accomplished? Any help is appreciated, thank you. Javascript jquery sql chat typing link|improve this question asked Jan 25 at 0:04Ian1207 62% accept rate.
Why don't we shift your thinking a little. Put the burden on the client, not the server, for the typing. After the first keypress, send an event to the server, and start a timeout.
After 5 seconds, we will say we aren't typing any more. If there is another keypress, we erase the old timeout, and start a new one. This will ensure that anytime a user is typing consistently, it is updated.
If your user reloads or leaves the page is the problem. Then they aren't typing, but the timeout won't trigger. To handle this, I would recommend that you do two things.
First, when the user requests a new page, always put their status to not-typing. Why? Because at the very least, at that second, they are not typing even if they are opening up a new tab.
Secondly, if the user leaves the browser and closes before the stopped typing trigger, you'll want to clean those up eventually. You could probably have a cron run every hour and any that are older than 5 minutes yet say they are still typing, set to not typing.
I don't understand how to "send an event to the server". The only way I know how to do something for one person and let another person see it is using the database... Could you elaborate a bit? Or send me a reference?
– Ian Jan 25 at 1:01 @itdoell Really, what I mean by that is use AJAX to send a request to a page on the server and then, from that page, query to the database. If you don't quite know how to use AJAX, there a lot of resources around and I can point you to some if you need. – Ktash Jan 25 at 1:03 Oh I know how to use AJAX, I better understand what you mean now.
Thank you. – Ian Jan 25 at 1:33.
Every five seconds or so, check if the contents of the text input are blank. If there is text, mark the user as typing. The only disadvantage is this is that leaving a message half-typed will mark the user as typing until they either close the window, realise and come back to send it, or clear it out.
But you can detect whether the half-typed message has changed or not before sending to the server.
Seems like a lot of overhead for something so simple, even if you are long polling (I hope). It really depends on the backend system connecting to sql. Have you considered using a web socket to do this?
It is true full duplex communication.
You could offload the typing state to something like memcache or redis as it doesn't matter if you lose the typing state for a user. And as you only need to read the value to tell others if the user was typing in the last 5 seconds.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.