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?
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.
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.
Openshift cloud platform supports websockets, and Java (Jboss, Spring, Tomcat & Vertx), PHP (ZendServer & CodeIgniter), Ruby (ROR), Node. js, Python (Django & Flask) plateforms.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With