views:

343

answers:

6

Can someone illustrate how I can get typing status of the other party with javascript?

UPDATE

Can someone recommend a one-to-one open source chatting application,preferably written in PHP? I only found open source chatting rooms which are for chatting among all onliners,but I just need a one-to-one chatting.

+1  A: 

This is an update to reflect the significant change in the OP's question:

Google Chat and Facebook both use XMPP(jabber) servers, as do most companies I know of that have internal instant messaging.

The nice part about XMPP is that you get all of the "is typing" and other presence-based information without having to roll-your-own in javascript (bear in mind, you will still need to use javascript to pass XMPP requests back to the server, but XMPP has most of the features you'd need already built in).

Check out OpenFire. It's a great XMPP server, totally open source, and they have a web-based version of their Spark client that is pretty nice.

Or you could get a PHP library for XMPP (there are a few). But you'd still need to have the XMPP server running in the background for PHP to work with.

Here's a list of XMPP libraries for PHP from XMPP.org:

  • Eiffel
  • JAXL
  • Lightr
  • Missus
  • xmpphp

Or, if you want to keep things mostly browser-side, they also have a list of libraries for javascript:

  • dojox.xmpp
  • js.io
  • JSJaC
  • strophe.js
  • xmpp4gwt
  • xmpp4js
Anthony
You are listening the event of your own side,what's that for?
I'm not sure I understand. Do you think there is some way in javascript to just see if people are typing at a chat site you are at? My solution assumes you are building a chat site.
Anthony
There is no legitimate way way to see another user's activity unless the site builder added that feature. anything else would be spying/hacking.
Anthony
Can you recommend a one-to-one open source chatting application,preferably written in PHP? I only found open source chatting rooms which are for chatting among all onliners,but I just need a one-to-one chatting.
That depends on what you mean by one-to-one. Do you mean something like what Facebook has? Where it's similar to AIM or Google Chat? Or do you mean you want to host something where you will personally chat with only one person?
Anthony
Yes,chat with only one person.
getting keypress event is the smallest problem. He needs help in communication stuff. sorry, -1
naugtur
Sorry, I was writing you more when my computer crashed last night. That was a dumb question. So Google Chat and Facebook both use XMPP servers, as do most companies I know of that have internal instant messaging. The nice part about XMPP is that you get all of the "is typing" without having to get any javascript that capture it. Check out OpenFire. They have a web-based version of their Spark client that is fairly nice. Or you could get a PHP library for XMPP (there are a few). But you'd still need to have the XMPP server for PHP to work with.
Anthony
@naugtur - I was the first to answer the question, and it was thanks to my answer that the OP was able to better articulate his goals. You'll notice I never said keypress was his largest problem, it just happened to be the problem he brought to this question. maybe you should save your energy for answering questions not critiquing people trying to help.
Anthony
Here is a list of XMPP libraries straight from xmpp.org :http://xmpp.org/software/libraries.shtml#php
Anthony
XMPP is used to chat between different IM like gtalk,msn...Not for a web chat,right?
XMPP is a chat protocol. When you host a jabber (xmpp) server, the server can handle chats just on that server, or it can send and receive chats from other xmpp servers.
Anthony
@Anthony - I said I'm sorry. I didn't try to criticize You. I did -1 for the sake of people looking through the answers. Your answer did help the guy, but it's no longer useful for others. I plussed Your answers in other questions.
naugtur
@nautigar - No hard feelings. I hope I wasn't too harsh with the response. I just get a twinge when I see down-votes, or even just comments, that are about some specific point and disregard the responder's overall good intentions or good ideas. Which isn't me patting myself on the back, I get a lot more upset when I see it on other members' answers. But obviously I jumped the gun in this case. Anyways, I hope the user ends up researching those XMPP libraries, as that is the best way to go.
Anthony
the "-1" undone after the rewrite.
naugtur
A: 

For example, if you had an text area #chat then you could use this code to attach the event:

document.getElementById('chat').addEventListener('keydown', FUNCTION HERE, false);
Delan Azabani
+3  A: 

Here are a list of PHP-based open-source instant messaging software.

Some of those might be relevant for you.

Stobor
A: 

I made a small chat application a while ago, and the only way to do it is to frequently check for new entries in the chat database and fetch anything newer than the last displayed message. At the same time as all that, you can check to see if the user's input is empty. If it is, do nothing. If it isn't, enter a status code into the database beside that user's name. If anyone has that status in the database when you're fetching information about new messages and who is online, you should display the 'user is typing' message. I hope that makes sense...let me know if it isn't.

Polling the server is not the best option. I did that too in some of my projects, but if chat is supposed to be an important feature - polling is EVIL. Use http streaming.
naugtur
I had a little look around and it seems really interesting, but a bit over my head at the moment. I've only been doing web dev for about a year, and JavaScript for 6-7 months. I'll give it a go, though.
A: 

For User1: If you save the chat message on each key-press to the database, with a status: sent=false and update the last updated date. For User2: you could pole periodically for the presence of a message where sent=false and use the last updated to update user is typing message. if the lastupdated date is more than a say ten seconds you could remove the message as that person may have stopped typing. This will allow User2 to see User1 typing, stopping and continueing again.

Ideally polling for this information will be part of an existing call to the database to reduce additional overhead.

Mark Redman
Your idea does even more polling... Any server other than localhost will not respond quick enough for it to be reasonable.
naugtur
@naugtur: With any chat there will be polling, thats why I indicated doing this on the existing poll to reduce any additional overhead. It may be that this problem doesnt actually need a solution, considering the overhead, but if a browser window is only servicing the chat with no other tasks, additional polling might be fine.
Mark Redman
polling will only happen server-side. This idea in Your last sentence is a good thing. I didn't drop a -1 on this, right? ;) But: `If you save the chat message on each key-press to the database` suggests sending A HUGE LOT of requests to the server. A request should be send when user starts typing and when he stops typing - with some additional time tolerance. This is realy important
naugtur
@naugtur: you'r right, each key-press would be a bit much. Saving on start and end would be better and I guess saving on each poll where each existing poll that Posts and Gets info that would be good compromise.
Mark Redman
+1  A: 

See http streaming and some ready solutions here: http://ajaxpatterns.org/HTTP_Streaming

this is how google talk does it. And there are ready php or c++ solutions

It was quie a discovery for me!

naugtur

related questions