The forehead.. How w did you end up with Nodejs? Websocket is just a protocol. Let me answer them one by one

WebSocket is a protocol from HTML5, which means the HTTP protocol has not changed, or it is ok, but HTTP does not support persistent connections (long connections, circular connections do not count).





There’s some overlap, but not all of it. Html5 also refers to a set of new apis, or new specifications, new technologies. The Http protocol itself is only 1.0 and 1.1 and is not directly related to Html itself. In layman’s terms, you can use HTTP to transfer non-HTML data, and that’s it. Again, the hierarchy is different.

First of all, Websocket is a persistent protocol, compared to HTTP, a non-persistent protocol. For a simple example, use the PHP life cycle, which is widely used today. 1) The HTTP lifecycle is defined by a Request, i.e., a Request and a Response. In HTTP1.0, this HTTP Request ends. Improvements were made in HTTP1.1 to have a keep-alive, that is, multiple requests can be sent and multiple responses received within an HTTP connection. But remember that Request = Response, which is always the case in HTTP, means that there can only be one Response per Request. Moreover, this response is also passive and cannot be initiated actively.

Coach, you BB so much, what does Websocket have to do with?






To borrow











GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
Copy the code





Upgrade: websocket
Connection: Upgrade
Copy the code

Note that the Websocket protocol is initiated, quickly help me find the corresponding assistant processing ~ not that old-fashioned HTTP.


Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Copy the code

First of all, sec-websocket-key is a Base64 encode value, which the browser randomly generates to tell the server: Peat, don’t be silly, I’m going to verify that Ni is really a WebSocket assistant. Sec_websocket-protocol, however, is a user-defined string used to distinguish between protocols required by different services under the URL. Simple understanding: Finally, sec-websocket-version tells the server to Draft the WebSocket protocol to use. At the beginning, the WebSocket protocol was still in the Draft stage, and all kinds of weird protocols were available. And there are a lot of weird things, like Firefox and Chrome not using the same version and so on, the Websocket protocol was a big problem at the beginning. Dehydration: Waiter, I want a 13-year-old oh →_→


HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
Copy the code


Upgrade: websocket
Connection: Upgrade
Copy the code

Still fixed, the Websocket protocol tells the client about the upcoming upgrade, not mozillasocket, lurnarsocket, or shitsocket. Sec-websocket-accept is an encrypted sec-websocket-key that is confirmed by the server. Server: good good, know, show you my ID CARD to prove the line. Sec-websocket-protocol is the final Protocol used.

At this point, HTTP has done all of its work, and now it’s all Websocket protocol. The details of the agreement are not explained here. —————— Technical analysis section is complete ——————














Before I talk about WebSockets, I’ll take a look at the principles of long Polling and Ajax polling. The first is Ajax polling, which is simple enough that the browser sends a request every few seconds to ask if the server has new information. Scenario Reappearance: Client: la la la, do you have any new information (Request) Server: No (Response) Client: La la la, do you have any new information (Request) Server: No (Response) Client: la la la, do you have any new information (Request) server: You are so annoying, no. (Response) Client: la la la, is there any new message (Request) server: ok la la, there is for you. (Response) Client: la la la, is there any new message (Request) server:… No… No… No (Response) —- loop

Long poll In fact, the principle of long poll is similar to that of Ajax polling, which adopts the polling method, but adopts the blocking model (keep calling, do not hang up if you do not receive the call). That is to say, after the client initiates a connection, if there is no message, it does not return a Response to the client. It does not return until there is a message, after which the client establishes the connection again and the cycle starts again. Client: la la la la, do you have any new information, if not, please return to me when you have (Request) Server: Um… Wait till there is news. Response client: la la la la, do you have any new information, if not, wait for it to return to me (Request) -loop

It can be seen from the above that in fact, the two methods are constantly establishing HTTP connections and waiting for the server to process them, which can reflect another feature of HTTP protocol, passivity. What is passive, in fact, is that the server cannot actively contact the client, only the client initiates. To put it simply, the server is a lazy refrigerator (this is a joke) (can’t, can’t initiate a connection), but the boss has orders that if a customer comes, no matter how tired they are, they should be welcomed.

With that said, let’s talk about the above drawbacks (forgive me for talking so much OAQ). It’s easy to see from the above, however, both of the above are very resource-intensive. Ajax polling requires fast processing speed and resources on the server. (Speed) Long poll requires high concurrency, that is, the ability to receive customers at the same time. (Site size) So this can happen with ajax polling and long polling.

Client: la la la la, any new information?



Server: The monthly line is busy, please try again later (503 Server Unavailable)



Client:… All right, blah, blah, blah, anything new?



Server: The monthly line is busy, please try again later (503 Server Unavailable)





Client:


The server is busy: refrigerators, I want more refrigerators! More.. More.. I was wrong. That’s another meme.

— — — — — — — — — — — — — — — — — — — — — — — — — — get to the point, Websocket for us Through the example above, we can see that these two approaches are not the best way, need a lot of resources. One needs more speed, one needs more ‘phones’. Both of these will lead to an increasing demand for ‘telephones’. Oh, I forgot to mention that HTTP is a stateless protocol. (Thank you for pointing out OAQ in the comment area) In plain English, the server is a forgetful ghost because it receives so many clients every day. When you hang up the phone, it will forget all your things and throw all your things away. You have to tell the server again the second time.

So in this case, Websocket appears. He solved these problems with HTTP. First, passivity, when the server completes the protocol upgrade (HTTP->Websocket), the server can actively push information to the client. So the above scenario can be modified as follows. Client: la la la, I want to establish the Websocket protocol, the required service: chat, Websocket protocol version: 17 (HTTP Request) server: Yes, it has been upgraded to Websocket (HTTP Protocols Switched client) : Please push the information to me when you have it. Server: OK, I’ll tell you sometime. Server: Balabalabalabala Server: Balabalabalabala Server: ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha

The result is an endless stream of information that can be sent through a single HTTP request. (In programming, this is called a callback, that is, call me back when you have information, instead of me being stupid enough to ask you every time.) This protocol addresses the fact that synchronization is slow and very resource-intensive. So why does he solve the problem of consuming resources on the server? In fact, the program we use is through two layers of proxy, that is, HTTP protocol in the Nginx server parsing, and then passed to the corresponding Handler (PHP, etc.) for processing. To put it simply, we have a very fast operator (Nginx) who is responsible for forwarding problems to the appropriate customer service (Handler). The speed of the operator itself is basically enough, but every time it gets stuck in the customer service (Handler), there are always customer service slow processing. , resulting in insufficient customer service. Websocket solves such a problem. After it is established, it can directly establish a persistent connection with the operator. When there is information, the customer service will find a way to inform the operator, and then the operator will deliver it to the customer. This will solve the problem of slow customer service processing.

Retransmission of Identity Info



The processing time
Excessive traffic/time.



An HTTP handshake, so the entire communication process is set up in a connection/state



Customer initiative inquiry
The server (push) when there is information to send (of course, the client is still waiting for the initiative to send information.
Customer Service (Handler)






Can’t



Simulate a similar effect