19
10
|
So I'm building a small chat application for friends and, as I'm building it, I've been going back and forth about how to get information in a timely manner that is not manual or as rudimentary as forcing a page refresh.
That said, I've put in simple Ajax but alas, it's with constant hits to the server via a short timer. Someone suggested to me (on another thread of mine here on SO) about long/short polling etc, and as I researched, I ran across HTML5 websockets...
It SEEMS easy to implement and although not full spec yet (I think) (plus I think only some browsers support it) I'm wondering, in a HTML5 WebSockets vs AJAX long/short polling, what are the advantages/disadvantages to them?
Since I am learning, I don't want to learn both if one is better... They kind of do the same thing (I'm assuming) but I'm wondering are there certain scenarios where one would use one technique over the other? Or is HTML5 WS supposed to take over in general?
Any tips/relative links etc...I'd appreciate.
Thanks in advance.
| |||
add comment |
23
|
WebSockets - is definitely the future. Long polling is dirty workaround of preventing creating connections for each request like AJAX does. But long polling was created when WebSockets didn't existed. Now due WebSockets, Long Polling is going away.
I recommend to learn WebSockets.
Here is list of Browsers that supports WebSockets right now. I believe that in half a year all major browsers will have full support, and in one year it will become a standard. For now it is working well based on my experience in Firefox, Chrome, Safari, iOS, IE10.
As well some clearance:
Main advantage of WebSockets for server, is that it is not HTTP request (after handshake), but proper message based communication protocol. That allows you to achieve huge performance and architecture advantages. For example in node.js you can share same memory for different socket connections, so that way they can access shared variables. So you don't need to use database as exchange point in the middle (like with AJAX or Long Polling and for example PHP). You can store data in RAM, or even republish between sockets straight away.
Remember that WebSockets generally have very different approach of logic for networking, more like real-time games had all this time, and not like http.
| ||||||||
|
5
|
For chat applications or any other application that is in constant conversation with the server,
WebSockets are the best option. However, you can only use WebSockets with a server that supports them, so that may limit your ability to use them if you cannot install the required libraries. In which case, you would need to use Long Polling to obtain similar functionality. | ||||||||||||
|
3
|
An obvious disdvantage of polling over sockets is that it will open useless connections to your server when there is no new data on the server.
Sockets will only create a connection when a new event happened.
On servers that create a lot of events and where sockets will therefore throttle your connection you best use interval polling.
On servers with a low data exchange rate its often best to use sockets.
| ||
add comment |