Web Chat related knowledge

Source: Internet
Author: User
Tags session id

HTTP related knowledge

HTTP is a stateless, request, Response mode communication mode, that is, each time a user clicks through a browser page, you need to re-establish a connection with the Web server, and send their session ID to the server side to the server side to verify the identity of this user. To obtain data from a Web server, the client must proactively initiate a request and then receive a server-side return, and the server will not actively push messages to the client.

Based on the traditional Web server will only passively respond to this feature of client requests, to achieve the needs of Web real-time chat, there are several ways:

Polling (polling)

Polling, the client makes a request to the server to obtain some data. In order to get server-side events as quickly as possible, the polling interval must be as small as possible. But the disadvantage of this:

If the interval is small, the client browser makes more requests, and many of these requests do not return any useful data, wasting bandwidth and resources in vain.

Comet

Ajax with polling is very limited: it is not scalable and does not provide low-latency communication (as soon as the event arrives on the server side, they reach the browser side as fast as possible).

Comet is a Web application model in which requests are sent to the server side and maintain a long lifetime until a timeout or server-side event occurs. After the request is completed, another long-standing Ajax request is sent to the server side.

One advantage of comet is that each client always has a communication link open to the server side. The server can push events to the client by submitting a response as soon as the event arrives, or it can even accumulate and then continue sending. Because requests remain open for long periods of time, the server side needs special features to handle all these long-lived requests.

There are two main ways to implement Comet:

Ajax-based long polling (long-polling) mode

The browser makes an XML HttpRequest request, and after the server receives the request, it blocks the request until the data or timeout is returned, and the browser JS makes the request again after processing the request return information (timeout or valid data) and re-establishes the connection. During this time, the server may have new data arrived, the server will choose to save the data until the connection is re-established, the browser will take all the data out at once.

Forever lframe (Eternal lframe)

This technique involves a hidden lframe tag placed on a page that has the SRC attribute pointing to the servlet path back to the server-side event. Each time the event arrives, the servlet writes and refreshes a new script tag with JavaScript code inside it, and the content of the lframe is appended to the script tag, and the contents of the tag are executed.

WebSocket

A new generation of HTML standard HTML5 provides a network technology WebSocket for full-duplex communication between browsers and servers.

WebSocket is a completely new standalone protocol, based on the TCP protocol, which is compatible with the HTTP protocol, but does not fit into the HTTP protocol, just as part of the HTML5. The script is then given another capability: initiating a websocket request.

Unlike the HTTP protocol request response pattern, WebSocket has a handshake (openling handshake) process before the connection is established, and there is a handshake (Closing handshake) process before closing the connection. Once the connection is established, both sides can communicate in two directions.

(Unfortunately, Django doesn't support it at the moment)

Advantages and disadvantages of some of the above implementations

Polling:

The client periodically sends an AJAX request to the server, returns the response information and closes the connection as soon as the server receives the request

Pros: Back-end program writing is easier

Cons: Half of the requests are useless, wasting bandwidth and server resources

Suitable for small applications

Long polling:

The client sends an AJAX request to the server, the server receives the request, hold the connection until a new message returns the response and closes the connection, and the client finishes processing the response and sends a new request to the server

Pros: No more frequent requests in the absence of messages, low resource consumption

Disadvantage: Server hold connection consumes resources, return data order is not guaranteed, difficult to manage maintenance

Long connections:

Embed a hidden iframe in the page, set the SRC attribute of the hidden IFRAME to a request for a long connection, or take a XHR request, and the server can continuously input data to the client

Advantage: The message arrives immediately, does not send the useless request, the management is also relatively convenient

Cons: Server maintenance A long connection increases overhead

Flash Socket:

Embed a flash program JavaScript that uses the Scoket class in the page to communicate with the server-side socket interface by invoking the socket interface provided by this Flash program, which controls the display of the page after receiving the server-side message

Pros: Real instant communication, not pseudo-instant

Disadvantage: The client must install the Flash plugin, non-HTTP protocol, unable to automatically traverse the firewall

Web Chat related knowledge

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.