Home >Web Front-end >JS Tutorial >Introduction to socket.io learning tutorial in node.js (1)
socket.io provides real-time two-way communication based on events, so the following article mainly introduces relevant information about socket.io, and mainly introduces the basic knowledge of learning socket.io. Friends who need it can refer to it. Below Let’s take a look together.
Preface
Real-time data transmission between the Web client and the server is a very important requirement, but at the earliest it can only be passed AJAX polling implementation. Before the WebSocket standard was introduced, AJAX polling was the only feasible way (it was also possible through the Flash browser, but we will not discuss it here). The principle of AJAX polling is to set a timer to synchronize server data through AJAX at regular intervals. This method involves delays and places a heavy load on the server. It wasn't until 2011 that the IETF standardized WebSocket, a protocol for sending and receiving data based on TCP sockets. Nowadays, all major browsers support WebSocket.
socket.io separates the data transmission part to form engine.io. Engine.io encapsulates WebSocket and AJAX polling to form a set of APIs. It shields detail differences and compatibility issues, and realizes cross-browser/cross-device bidirectional data.
socket.io is not necessary for engine.io. You can also implement your own engine.io and bind it through server.bind
Application
Real-time data analysis display (reports, logs)
Instant messaging, chat,
Binary stream (picture, music, video) transmission
Multi-person collaborative editing
Instant Message push
Similar technology
AJAX polling: AJAX polling based on XMLHttpRequest
AJAX long polling: Similar to polling, the server does not return a response after the client requests it until there is data that needs to be transmitted to the client. After the data is transmitted The client initiates a new request. The disadvantage is that additional HTTP headers need to be transmitted, and it also requires some extra work to keep the request from responding.
HTTP stream: Similar to AJAX long polling, the server response will bring the HTTP header: Transfer-Encoding: chunked
, after returning the data to the client The client does not need to initiate a new request. The disadvantage is that the data between each chunk is difficult to identify and process.
Plug-ins: Similar to Flash and Silverlight, as plug-ins are increasingly rejected by mainstream browsers and users
Here are the different angles Analyze each technology
One-way/two-way
One-way: AJAX polling, AJAX long polling, HTTP streaming
Two-way: WebSocket, Plug-in
Delay
Summary
socket.io encapsulates WebSocket. If WebSocket is not supported, it also provides downgraded AJAX polling, which is fully functional. , elegantly designed, is the perfect way to develop real-time two-way communications.The above is the detailed content of Introduction to socket.io learning tutorial in node.js (1). For more information, please follow other related articles on the PHP Chinese website!