最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Server-side Events - Stack Overflow

programmeradmin1浏览0评论

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 munication 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: / , 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.

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 munication 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/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.

Share Improve this question edited Jun 8, 2009 at 12:46 manixrock asked Jun 8, 2009 at 12:12 manixrockmanixrock 2,5534 gold badges24 silver badges29 bronze badges 1
  • 1 The W3C hasn't added an HTML5 object for it because HTML5 would be the wrong place to include it. It would have to be included in the JavaScript standard, not the HTML standard. – Dan Herbert Commented Jun 8, 2009 at 13:59
Add a ment  | 

5 Answers 5

Reset to default 3

The situation here's changed a bit.

See screenshot:

All major browsers except IE - including Firefox, Webkit (i.e. Chrome plus Desktop/Mobile Safari), and Opera have had a chance to implement the HTML5 implementation of server-side push.

This is great news for iPhone/iPad/Android web app developers, but not so momentous for the rest of the world that is still struggling with poor web standards implementation of IE6-8, and Firefox playing catch-up.

The current situation is that if you want cross-browser support, then the Comet server described by Dan (or an iFrame/xmlhttp polling repeatedly) is still your best option.

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.

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.

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.

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 munication and from the applet call a JavaScript function to make updates to the web page DOM/HTML when new data bees available. The Java SDK is free. The Flash SDK isn't, last time I tried to find where to download it.

发布评论

评论列表(0)

  1. 暂无评论