LongPolling and Polling overview server push technology applications are becoming more and more common, application scope is becoming wider and wider, and technical solutions are becoming more and more mature and rich. Many SNS use LongPolling Technology for chat functions. For example, fackebook and kaixin001. The LongPolling principle is actually very simple and clever. Compared with Polling,
Long Polling and Polling overview server push technology applications are becoming more and more common, application scope is becoming wider and wider, and technical solutions are becoming more and more mature and rich. Many SNS use the Long Polling technology for chat functions. For example, fackebook and kaixin001. The principle of Long Polling is actually very simple and clever. Compared with Polling,
Overview of Long Polling and Polling
Server push technology applications are becoming more and more common, and the application scope is wider and wider, and technical solutions are becoming more and more mature and rich. Many SNS use the Long Polling technology for chat functions. For example, fackebook and kaixin001.
The principle of Long Polling is actually very simple and clever. Compared with Polling, the Long Polling client may not immediately receive a response from the server. It may take some time to wait until a new message or a timeout connection is established ). Similarly, the client does not need to send requests to the service at regular intervals. Instead, the client requests the client immediately after receiving the server response or after the connection is lost. Here, for example, the traditional Polling is generally asked by C to S: "is my letter ?". S will query the query immediately after receiving the inquiry and tell C about the query result. No matter whether there are any letters of C, I need a code to reply: "Well, you have X letters .", Reply by Code: "No, no letter". Long Polling is more like this. C sends a question to S: "Is there a letter for me ?", S starts querying. If yes, the system replies C: "Well, you have x emails ". If there is no such message, C waits for it to query the subscriber's messages over and over again. In other words, after S receives the C query request, Polling queries only once and tells C the query result. After Long Polling receives the request, it queries again and again, C is returned only when a message exists. Otherwise, the Client is always held.
Compared with traditional Polling, Long Polling reduces the number of requests. For example, a user may receive two new messages every two hours. If the passthrough Polling method is used, a query request is sent to the server every 30 seconds. In these two hours, the server needs to process 240 (60*60*2/30) requests, of which at least 238 are meaningless. Imagine, if it is 10000 concurrent, this waste is amazing. Long Polling is not a waste of server resources to process these meaningless requests.
Polling
The traditional Polling implementation method is relatively simple, and http requests are regularly sent by the client javascript script. The server script is as follows:
The above is a simple server script for traditional polling. After receiving the client request, the server immediately executes the script query and immediately responds to the client. The client waits for a short period of time. The only thing the client needs to do is to regularly send a query request to the server. The following is the packet captured through tcpdump during the request:
1>17:49:03.533760 IP 192.168.0.98.4383 > devhome.http: S 3235664319:3235664319(0) win 65535
2>17:49:03.534336 IP devhome.http > 192.168.0.98.4383: S 2018732723:2018732723(0) ack 3235664320 win 5840
3>17:49:03.533841 IP 192.168.0.98.4383 > devhome.http: . ack 1 win 655354>17:49:03.534404 IP 192.168.0.98.4383 > devhome.http: P 1:781(780) ack 1 win 655355>
17:49:03.534416 IP devhome.http > 192.168.0.98.4383: . ack 781 win 70206>
17:49:03.535033 IP devhome.http > 192.168.0.98.4383: P 1:369(368) ack 781 win 70207>17:49:03.535110 IP devhome.http > 192.168.0.98.4383: F 369:369(0) ack 781 win 70208>17:49:03.535263 IP 192.168.0.98.4383 > devhome.http: . ack 370 win 651679>17:49:03.536105 IP 192.168.0.98.4383 > devhome.http: F 781:781(0) ack 370 win 6516710>17:49:03.536111 IP devhome.http > 192.168.0.98.4383: . ack 782 win 7020
Rows 1st, 2, and 3, and TCP three-way handshake to establish a connection.
Line 3: httpd request sent from 192.168.0.98 to devhome.
Line 3: The devhome server confirms that it has received an http request from the client 192.168.0.98.
Line 3: the server responds to the httpd request sent by client 192.168.0.98 after devhom. Note: pay special attention to the time cut in the first column. The time interval between the http request and the http response is very short, only 0.001 s.
There are four rows in total: 7th, 8, 9, and 10. TCP waved and disconnected. The server proactively disconnects the connection.
Long Polling
Long Polling is slightly different from Polling. Long Polling is executed continuously to delay the response to the client. Check the Code:
'Info', 'c' => $ msg); flush (); exit (0);} usleep (3000000 );} echo json_encode (array ('T' => 'refresh', 'c' => 3000); flush ();?>
The above is the Long Polling server code. The meaning is also clear. If $ msg exists, it will immediately respond to the client request and close the TCP connection. If no $ msg exists within $ timeout, the client will keep the TCP connection unchanged (closed ). Until the value of $ timeout is exceeded (the specific time mainly depends on $ timeout * $ usleep_time), the server will require the client to re-Request (re-establish a TCP connection) and close the current TCP connection. The following is the packet captured through tcpdump:
1>18:39:46.449563 IP 192.168.0.98.4407 > devhome.http: S 174149200:174149200(0) win 65535
2>18:39:46.449587 IP devhome.http > 192.168.0.98.4407: S 938669730:938669730(0) ack 174149201 win 5840
3>18:39:46.449692 IP 192.168.0.98.4407 > devhome.http: . ack 1 win 655354>18:39:46.450308 IP 192.168.0.98.4407 > devhome.http: P 1:793(792) ack 1 win 655355>
18:39:46.450320 IP devhome.http > 192.168.0.98.4407: . ack 793 win 71286>
18:42:46.521749 IP devhome.http > 192.168.0.98.4407: P 1:377(376) ack 793 win 71287>18:42:46.521825 IP devhome.http > 192.168.0.98.4407: P 377:412(35) ack 793 win 71288>18:42:46.521859 IP devhome.http > 192.168.0.98.4407: F 412:412(0) ack 793 win 71289>18:42:46.521997 IP 192.168.0.98.4407 > devhome.http: . ack 412 win 6512410>18:42:46.522021 IP 192.168.0.98.4407 > devhome.http: . ack 413 win 6512411>18:42:46.522965 IP 192.168.0.98.4407 > devhome.http: F 793:793(0) ack 413 win 6512412>18:42:46.522970 IP devhome.http > 192.168.0.98.4407: . ack 794 win 7128
Rows 1st, 2, and 3, and TCP three-way handshake to establish a connection.
Line 5: 192.168.0.98 sends an http request to the server 192.168.0.6.
Line 5: 192.168.0.6 confirms that the request sent by 192.168.0.98 is received.
Lines 6th and 7: The server host_6 responds to the http request sent by the client 192.168.0.98 three minutes ago. NOTE: For the time truncation In the first column, the difference between 5th rows and 6th rows is 3 minutes. This is in line with the server script and client monitoring.
In the last four rows, TCP waves and disconnects, which is also initiated by the devhome server.
Visible (drawn by httpwatch), it is verified that the response to the http request from the client host_98 will be 3 minutes later. This also shows that the difference between Long Polling and Polling is that the client may have to wait a longer time to receive the response from the server.