Can HTML5 WebSockets be used for tasks typically done with AJAX?

I just started learning HTML5 WebSockets . I was wondering if I can update the entire contents of a webpage using websockets instead of using ASP.NET UpdatePanels, or would it be redundant?

Can WebSockets be used to replace AJAX? And why use WebSockets?

Most examples are for bidirectional chat demos. But if I wanted to click a button rather than revert back to refresh the grid, can I do this with WebSockets and would that be a good idea?

+6
source share
4 answers

I think XHR and WebSocket are for two different scenarios, and you should use the one that works best for your scenario.

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

WebSocket solves the problem above. You can send a request without waiting for a response. The server can also send you something through a response without first initiating a request.

When you click a button and update the content (for example, editing table cells) XHR (and UpdatePanel) works better. This is because updating content must be a click away. This is a couple of request-response. But in a script for updating pure content (for example, when displaying stock prices in real time) WebSocket works better. In a scenario in which updating the content is not related to a button click (for example, in chat), WebSocket also works better.

+3
source

WebSocket standards are designed for applications that require low latency, low connectivity. They are good for existing applications that push the limits of what's possible with AJAX / Comet / long-poll. But more importantly, WebSockets will allow you to create a whole new class of web applications that do not yet exist.

In your case, it looks like WebSockets is likely to be excessive, since latency is not the main problem in what you are building. Of course, you can do this using WebSockets, but I suspect that this will be additional work for a very small gain in your case.

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

+2
source

If you want to use WebSockets on 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 that this flourish for games in the browser, but the server-side implementation is not yet fully standardized - there may be additional security complications.

Currently, WebSockets are used only for toy implementations. They are not ready to work with clients. In addition, they are really only needed when Ajax calls and Comet calls are too slow for your needs.

0
source

Technically, yes. In practice, I will probably wait.

Websockets are definitely the way HTML5 uses the type of connection we're used to. Technically, yes, you can, but depending on the type of site you are building, you may want to stop. Websockets is one of the new parts of the HTML5 specification and is still being finalized. It works in the latest versions of Chrome and Firefox 4, but IE9 hasn't implemented it yet, and there is no word about whether IE10 will have it. Technical sites that demonstrate the latest technology (for example, a demonstration of what is possible in HTML5), and any other things in which the vast majority of the audience will be guaranteed to use a supporting browser or early users, should be in order. If not, you can push some users away. Only you can decide where to go.

The key point here is that Websockets is currently a changing specification, and AJAX works in both old and new browsers. If you want to get backward compatibility in addition to the warranty, specifications and browsers will not change tomorrow and violate your existing code, use AJAX. If you're cool with a small chance that specifications and browsers might change in the future and don't care about people using older browsers, then use websites.

fooobar.com/questions/4796 / ... :

  • Chrome 4.0 supports websites.
  • Safari 5.0.2 also supports them.
  • Firefox 4.0 comes with WebSockets support disabled. to view it, see
  • Opera 11 comes with support disabled to enable it again, see
  • IE9 does not support them, however the add-in offers experimental support
0
source

Source: https://habr.com/ru/post/891215/


All Articles