6

I just started researching HTML5 WebSockets. I was wondering whether I can update all of a web page's content with websockets instead of using ASP.NET UpdatePanels, or would this be overkill?

Can WebSockets be used as a replacement for AJAX? And is this what WebSockets should be used for?

Most of the examples are for bi-directional chat-like demos. But if I wanted to click a button and not postback to update a grid, could I do this with WebSockets and would that be a good idea?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
user812368
  • 83
  • 1
  • 6

4 Answers4

4

I think XHR and WebSocket are for 2 different scenarios and you should use the one better fits your scenario.

XHR has the request-response pair. Each request is paired with a response. This is good for remote procedure call, but creates unnecessary overhead if you want response without request (i.e. server push).

WebSocket solves the problem above. You can send a request without expecting any response. The server can also send you anything via response without you initiating a request first.

In a button clicking and content updating scenario (e.g. editing table cells), XHR (and UpdatePanel) works better. That's because the content updating has to be paired with a button clicking. This is a request-response pair. But in a pure content updating scenario (e.g. displaying real-time stock price), WebSocket works better. In a scenario in which content updating isn't related to button clicking (e.g. chatting), WebSocket also works better.

Cat Chen
  • 2,387
  • 17
  • 12
3

The WebSocket standards are designed for applications that need low-latency, low-overhead communication. They are good for existing applications that are pushing the limits of what is possible with AJAX/Comet/long-poll. But even more importantly, WebSockets will enabled a whole new class of web applications that don't exist yet.

For your case, it sounds like WebSockets would probably be overkill since latency isn't a core issue in what you are building. You certainly could do it with WebSockets, but I suspect that would be extra work for very little gain in your case.

See this answer for why WebSockets are already ready for general use (with web-socket-js and native iOS support this means that WebSockets are supported on pretty much all browsers in the wild).

Community
  • 1
  • 1
kanaka
  • 70,845
  • 23
  • 144
  • 140
  • The main problem with using WebSockets in this scenario are that quite a bit of effort would probably need to be undertaken for just a bit of gain. In this scenario it's probably not worth trying to add WebSocket support to IIS (Assuming the ASP.NET app is running on IIS) or adding an additional WebSocket server and infrastructure to allow the ASP.NET app to talk to it. I completely agree that WebSockets are ready for general use due to the fallback support. The problem is setting up WebSocket support on the backend in **your own hosting environment** is a bit of a pain at the moment. – leggetter Jun 24 '11 at 13:58
0

If you want to use WebSockets in a page that IE8 should be able to view, use Ajax instead.

WebSockets were originally designed for fast, two-way communication between client and server. It was very important to have this for games to flourish in the browser, but the server-side implementation is not fully standardized yet -- there may be further security complications.

Right now, WebSockets are only used for toy implementations. They are not customer-ready. Also, they really are necessary only when Ajax calls and Comet calls are too slow for your needs.

Thaddee Tyl
  • 1,126
  • 1
  • 12
  • 17
  • By adding [web-socket-js](https://github.com/gimite/web-socket-js) (Flash based polyfill/fallback) to your application you can support any browser with Flash. Combine that with the fact that iOS supports WebSockets natively and that makes WebSockets available on just about all major browsers in the wild. There are numerous WebSockets servers and most support all protocol versions that are in browsers. Also, "toy" is misleading. While it's true that most WebSockets use is relatively new and in development, there are certainly applications of it that are production quality. – kanaka Jun 23 '11 at 15:55
0

Technically, yes. Practically, I'd probably wait.

Websockets are definitely the way HTML5 does the type of communication we're used to. Technically, yes you can, but depending on the type of site you're building, you might want to hold off. Websockets is one of the newer pieces of the HTML5 spec and is still being finalized. It works in the newest versions of Chrome and Firefox 4, but IE9 doesn't have this implemented yet, and there's no word on if IE10 will have it either. Technical sites that show off the newest technology (like demoing what is possible in HTML5) and any other things where the vast majority of the audience will be guaranteed to use a supporting browser or are early adopters should be fine. If not, you may be alienating some users. Only you can decide which way to go.

The key here is that Websockets is a changing spec currently, and AJAX works in both old and new browsers. If you want backwards compatable in addition to a guarantee the spec and browsers won't change tomorrow and break your exisisting code, use AJAX. If you're cool with a slight possibility that the spec and browser implementations may change in the future and don't care about people using older browsers, then use websockets.

Another stackoverflow answer shows websocket support:

  • Chrome 4.0 supports Websockets.
  • Safari 5.0.2 supports them too
  • Firefox 4.0 ships with support for WebSockets disabled. to enable it see
  • Opera 11 ships with support disabled to re-enable it see
  • IE9 does not support them, however an add-on offers experimental support
Community
  • 1
  • 1
Ryan Hayes
  • 5,290
  • 4
  • 42
  • 52
  • The WebSocket API (which is a separate standard from the protocol itself and also not technically part of HTML5) is unlikely to change in backwards incompatible ways. The WebSocket protocol allows servers to be written which support multiple versions of the protocol and most do. iOS now supports WebSockets natively. Also, [web-socket-js](https://github.com/gimite/web-socket-js) is a Flash based polyfill/fallback. This means browser support is nearly universal already. – kanaka Jun 23 '11 at 15:49