How Facebook chat is working?

前端 未结 2 1821
[愿得一人]
[愿得一人] 2021-02-13 19:54

How Facebook chat is working? Can anyone give me idea? I mean they are using websocket or AJAX? How they have implemented it?

2条回答
  •  深忆病人
    2021-02-13 20:27

    The user establishes a WebSocket connection through a process known as the WebSocket handshake. This process starts with the user sending a regular HTTP request to the server. An Upgrade header is included in this request that informs the server that the user wishes to establish a WebSocket connection. WebSocket URLs use the ws scheme. There is also wss for secure WebSocket connections which is the equivalent of HTTPS. If the server supports the WebSocket protocol, it agrees to the upgrade and communicates this through an Upgrade header in the response. Now that the handshake is complete the initial HTTP connection is replaced by a WebSocket connection that uses the same underlying TCP/IP connection. At this point either party can starting sending data.

    With WebSockets you can transfer as much data as you like without incurring the overhead associated with traditional HTTP requests. Data is transferred through a WebSocket as messages, each of which consists of one or more frames containing the data you are sending (the payload). In order to ensure the message can be properly reconstructed when it reaches the client each frame is prefixed with 4-12 bytes of data about the payload. Using this frame-based messaging system helps to reduce the amount of non-payload data that is transferred, leading to significant reductions in latency.

提交回复
热议问题