Contact ajax live chat -websocket distinguished from

 Ajax is what?

Ajax, or Asynchronous JavaScript and XML, is a web development technology to create interactive web applications. By exchanging small amounts of data with the server behind the scenes, Ajax can make asynchronous page updates, which means that, on the part of the web page to load updated without reloading the entire page.

Ajax advantage is that it uses asynchronous data transfer (HTTP request), the user is not blocked between the browser and the web server, the core object is XMLHTTPRequest. By this object, js can exchange data with the web server without reloading the page.

What websocket that?

websocket HTML5 is a new protocol to achieve full-duplex communication with the server browser. Its essence is: to create the first handshake via HTTP / HTTPS protocol for exchanging data TCP connection, the server and the client TCP connection to this real-time communication.

websocket biggest advantage is that - servers and browsers can be any time within a given time range, each push message. After the connection is established, the server can take the initiative to transmit data to the browser. Ajax and the biggest difference is that websocket: Ajax requires the client initiated the request, websocket server and the client can push messages to each other in real time.

The difference between Ajax and polling of websocket

Real-time data interaction between the browser and the server, through the establishment of websocket, also Ajax polling way through.

Understand the basic concepts of Ajax and websocket, then what we would usually apply to scenarios in both technology?

Ajax application scenarios
Ajax asynchronous interaction is characterized by dynamically update the web page, so the scope of application of Ajax is more interactive, web applications frequently read data.

Ajax scenarios:

1. form data verification Ajax

When filling out the form, the need to ensure the uniqueness of the data (for example, new user registration fill in the user name), it is necessary for data validation of user input.

Using Ajax technology, may be issued by the XMLHttpRequest object verification request, according to the HTTP response returned determine whether authentication is successful, the whole process does not require a new pop-up window, the entire page does not need to be submitted to the server quickly and will not increase the burden on the server.

2. The data fetch on demand

In web applications, often used classification tree or tree structure, such as departmental structure, file classification structure.

Ajax technology is implemented as a tree structure:

At initialization page, where only data of the first sub-categories and display stage; when the user points to open a node classification, all data in the current page will request two sub-categories belong to the classification server through Ajax; if when another request has been presented secondary classification of a node, again requesting all data three subcategories of the current classification belongs to the server, and so on. Page request to the data server will need, so you do not redundant data according to user's operation, reducing the amount of data downloaded. At the same time, you do not need to reload the entire page content is updated, update only that part of the content needs to be updated to greatly shorten the waiting time for users.

3. Automatic Updates page

web applications have a lot of data changes in real time, for example: latest news, weather and chat rooms, and so on. Before Ajax appear, even if the user to understand the appropriate content must be constantly refresh the page to see if there are new changes in the content, or refresh the page itself to achieve timing functions.

Ajax applications this technology can improve this situation, after the page loads, will conduct regular polling in the background by Ajax, the server sends a request to see if the latest news (of course, there are drawbacks in this case, said above Arrived). If there is new data will be downloaded and updated dynamically on the page, through a certain way to inform the user.

websocket application scenarios

Need to decide whether to use websocket art method is very simple:

Your product needs to provide multiple users to exchange function? Your product needs to show constantly changing data on the server side of it? If the answer is yes, then your product will apply websocket technology.

Common scenario is as follows:

1. The social networking application

At a benefit for the application of social classes it is - instantly know what your friends are doing? Users will not want to after a few minutes in order to know the red envelope sent by a family member or a friend in the group to send you a message. The user is online, so messages that users receive should be in real time.

2. Application of the equity funds category price

Rapidly changing financial world - almost all changes happen every millisecond, outdated information can result in a loss. When we open a stock or fund-type applications, we want to know the price of goods in real time, rather than 10 seconds before the data. Use websocket can update the data changes in real time without having to wait.

3. Location-based applications

More and more location-based applications, are borrowed from GPS-enabled mobile devices to achieve. If you have been recording the user's location, you can collect more detailed oriented data. If you need to update the network data in real time dashboard (for example: the coach said athletes need to look at these data), to borrow websocket can be done to make real-time data refresh.

4. online educational applications

Online education is a good way to learn, and teachers and other students can exchange. websocket technology multimedia chat, text chat.

to sum up

Ajax usually more interactive applications, web applications frequently read data.

websocket typically applications that require multiple users to exchange, or need real-time data display server changes in both cases. Understanding of these two technologies, in the design of the relevant product features, will be able to respond freely up.

Guess you like

Origin www.cnblogs.com/starluke/p/11728770.html