Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use HTML5 WebSockets for tasks usually accomplished with AJAX?

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?

like image 963
user812368 Avatar asked Jun 23 '11 14:06

user812368


People also ask

Does AJAX use WebSocket?

Response data from the AJAX service is encoded as JSON. The server sends the JSON response data to the browser via the WebSocket connection. The AJAX response is managed in the browser.

When should you not use a WebSocket?

Avoid using WebSockets if only a small number of messages will be sent or if the messaging is very infrequent. Unless the client must quickly receive or act upon updates, maintaining the open connection may be an unnecessary waste of resources.

Which of the following depends on support for HTML5 WebSocket?

Openshift cloud platform supports websockets, and Java (Jboss, Spring, Tomcat & Vertx), PHP (ZendServer & CodeIgniter), Ruby (ROR), Node. js, Python (Django & Flask) plateforms.

What is the difference between AJAX and WebSocket?

Ajax uses the HTTP Protocol and can send requests using POST/GET methods from Client to Server. WebSocket is itself a protocol to communicate between Client and Server, distinct from HTTP. In Ajax when you send a request , server sends response for that request and connection ends.


1 Answers

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.

like image 89
Cat Chen Avatar answered Nov 01 '22 15:11

Cat Chen