views:

308

answers:

4

I'm looking for the fastest way for multiple users on different machines to interact, and I think it's by using the server as the communication facilitator. So user A and user B both create a socket connection to the server of some kind and whenever user A does something it sends a message to the server, which then sends a message to user B.

The later part is what I'm trying to do in javascript. I've already done it in flash with XMLSocket, so is there an equivalent functionality in javascript?

PS: I found EventSource() here: http://dev.w3.org/html5/eventsource/ , but I'm not sure if it's implemented by browsers yet.

Edit: Ideally it would be used for things like chat applications where real-time events are needed, so using ajax calls every X seconds would be too slow. Why hasn't W3C added such an HTML 5 object yet, since it seems to be very useful.

+2  A: 

You might want to look into Comet (reverse Ajax) for generating server-sent events. As far as I know, no browsers implement EventSource().

The Ajaxian also has a good article about Comet including examples such as GTalk.

This option can be considered "bad" because it keeps a connection constantly open with the server which can increase load.

You could consider polling by making an Ajax request every X seconds to check for an update. As mentioned in the Ajaxian article I included, 37Signals polls every 3 seconds for real-time chat and that seems to work "good enough" for them.

Dan Herbert
A: 

Well, you have ajax. That does not keep an open connection (except in the case of keep-alive connections, but js cannot work with them like sockets). You send an XML-encoded message to the server from A, and B regularly polls the server for messages. The server would have to keep an inbox for each client.

Jeff Ober
+1  A: 

One way to do this is to use a flash-javascript socket bridge. There are several implementations of this. I've used this one. It is very fast! Another benefit to this is that you won't have to change your server to make it work.

CEL
A: 

Flash-JavaScript? How about Java-JavaScript (I.e. LiveConnect). I'm creating a page right now where I use a hidden Java applet (width and height 1 pixel with the same color as the background) to have full control over all the good low-level socket communication and from the applet call a JavaScript function to make updates to the web page DOM/HTML when new data becomes available. The Java SDK is free. The Flash SDK isn't, last time I tried to find where to download it.