The difference and principle of WebSocket and HTTP

Source: Internet
Author: User

Sometimes there will be old iron in what kind of environment to consider using websocket, what kind of environment use HTTP, today we say next WebSocket and HTTP.

The difference between WebSocket and HTTP

HTTP protocol is used in the application layer of the Protocol, he is based on the TCP protocol, the HTTP protocol to establish a link must have three handshake to send information.

HTTP links are broken down into short links, long links, and short links are three handshakes each time a request is sent to send their own information. That is, each request corresponds to a response. A long link is a link that stays within a certain period of time. Keep the TCP connection constantly on. The client communicates with the server, and the client must initiate and the server returns the results. The client is active and the server is passive.

WebSocket He was trying to solve the problem that the client initiates multiple HTTP requests to the server resource browser that has to take a long time to rotation, and he implements Multiplexing, which is full-duplex communication. The client side and the browser can send messages at the same time under the WebSocket protocol.

After the Wensocket is established, the server does not have to send a request to the browser after the browser sends requests. At this point the server has the initiative to send information to the server when it wants to be sent. And the information does not need to have part of the head with the information of the long-link HTTP communication, this way, not only to reduce the pressure on the server. And the information also reduces some of the unnecessary information.

Long connection of HTTP with WebSocket persistent connection

HTTP1.1 connections are using long connections by default (persistent connection),

That is, within a certain period of time to maintain the link, the client will need to the server in a short period of request a large number of resources, keep the TCP connection continues to open. The client communicates with the server, and the client must initiate and the server returns the results. The client is active and the server is passive.

Multiple Request/response message pairs can be transmitted on a TCP connection, so it is still the request/response message pair, which will cause the waste of resources and the problem of not being strong in real time.

If it is not a continuous connection, that is, a short connection, then each resource to establish a new connection, the HTTP underlying use of TCP, then each time to use a three-time handshake to establish a TCP connection, that is, each request corresponding to a response, will cause great waste of resources.

Long polling, that is, the client sends a request with a long timeout, the server hold this connection and return response when new data arrives

Persistent connection of WebSocket

Only one Request/response message pair is established, followed by a TCP connection, which avoids the redundant header information that is required to establish the Request/response message pair multiple times.

Hopefully speaking so much can help you learn more about the differences and connections between WebSocket and HTTP. If you still do not understand can try a third-party websocket service, similar to goeasy this free, you can go to understand. The website seems to be Www.goeasy.io, so many yards ... That's what we're talking about here today.

WebSocket and HTTP differences and principles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.