Socket. IO is definitely the most amazing of the small library frameworks I’ve worked with, as it can implement simple chat applets with just a few lines of code. I’ve been looking for an opportunity to use it in development for a long time, and recently I was writing a project when the need came up. Before the official use of the first to do a little research, and wrote a small demo, here to record a little experience.

What is a socket. IO

Socket.IO is a class library supporting real-time bidirectional communication based on events. It can work on any platform, browser or device, while ensuring reliability and speed, and can build real-time applications. It is extremely compatible, downgrading for incompatible environments, and supports browsers as low as IE5.5.

Why socket. IO, how it works, and why it enables real-time communication. To understand socket. IO, start with the basics of networking.

From polling to WebSocket

Let’s start with a relatively new network application layer protocol: WebSocket. In traditional network applications, HTTP is used in most scenarios. Are there any problems that cannot or cannot be easily handled by HTTP? Consider a scenario where the server wants to push messages to the client. Under the HTTP protocol, network communication can only be initiated by the client to the server, the server is unable to actively push messages to the client, the client to receive messages from the server, it has to keep sending requests to the server, this way is called polling. Polling is very expensive, because the client always has to ask the server whether there is a message or not, which is inefficient and wastes resources, obviously it is not a good solution. There is also long polling, in which the client sends a request and waits until the server returns before establishing a new connection. It also takes up unnecessary resources. The root of all this is that there is no client request server is unable to communicate with the client, this is the defect of one-way communication, we need a technology to achieve two-way communication between the client and the server.

Websocket is to solve this problem, now has been written into the standard, the mainstream browser basic support. Websocket is also built on TOP of TCP, the request protocol is WS or WSS (encryption), and the address writing is basically the same as HTTP, as shown below

ws://server.example.com/chat

This is a WebSocket request. The request header looks something like this:

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.comCopy the code

It looks like HTTP, because the handshake phase is HTTP, but the Upgrade is added to the request, and the corresponding information is as follows:

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

The server also responds to the Upgrade. At this point, it has nothing to do with HTTP. After the protocol Upgrade, the two parties establish a Websocket connection. With only one connection established, the client and server can now communicate with each other at the application layer in full duplex, as shown in the figure below:

The realization of the socket. IO

IO to achieve two-way communication, of course, Websocket is an essential technology, but socket. IO is not only the encapsulation of Websocket, in the environment that does not support Websocket, socket. IO also has a variety of polling solutions, to ensure that it can work properly.

Socket. IO makes what might seem like a complex, hard-to-implement job very easy. Its API is concise and useful in many real-time scenarios.

For a quick example of socket. IO, see this article