What are Network Sockets and Websockets?

Asked

Viewed 10,012 times

29

I am studying real-time Node.js programming and I have arisen some doubts regarding network sockets. I found nothing explained in detail, so I decided to open the question.

What is a network socket and a Websocket? What is the difference between the two? Is HTTP communication based on a request-response other than in real time given through a socket? What is the role of the TCP protocol in sockets and Websockets?

  • 1

    I have exactly the same question. I used HTML5’s Websocket, created a chat, but still, I don’t know exactly differentiate the definitions (regarding concepts and such)

2 answers

21


Websockets and Sockets are both client-server communication protocols, but although they have certain similarities (besides the name), they are distinct specifications for different purposes as well.

Sockets

The protocol Socket is used for communication between processes over the network, transmitting data in binary format. For network communication, it operates on some protocol such as TCP or UDP to abstract the inherent complexities of the transmission medium. Each Socket connects to a specific port. Sockets are generic solutions, commonly used for high-performance communication across different platforms.

Websockets

The protocol Websocket, in turn, has the much more specific purpose of being implemented in web browsers for persistent communication of Javascript code with the web server, usually communicating through the port 80 and always on the protocol TCP. The protocol format is in text and the Handshake that initiates communication is much like HTTP, so web servers can easily serve HTTP and Websockets on the same port.

  • The Socket is persistent?

  • @ropbla9 Sockets TCP are due to the very nature of the protocol, which maintains connection status. UDP sockets don’t actually connect to any other host, they just send streams of data over the network, so we could say that they are not persistent (nor would it make sense to be).

  • then the connection between two sites is not persistent due to the nature of HTTP, and if it depended on TCP, it would be?

  • 1

    @ropbla The connection between the browser and the server is persistent at a low level in the sense that it persists until the end of the request. But the highest level HTTP protocol is not persistent because after you request and get a response, it will terminate the TCP connection.

  • @utluiz You have already made integration with websocket?

12

Sockets are the ends of a communication between two processes (applications) through a network of computers. In the operating system these ends are resources that the system creates upon request of the processes so that they can communicate over the network. As today most communication between computers is based on Internet Protocol, most of the sockets sane internet sockets. From the programmer’s point of view they are accessed through an API (from the operating system or indirectly through a library) that makes it possible to establish communication with a "socket address" (a unique combination of an IP address and a port number).

Two types of socket are more common, the stream sockets (that underneath the cloths use the TCP protocol in the transport layer, so I usually call them TCP sockets) and the Datagram sockets (which use the UDP protocol). The difference between them is that the TCP sockets keep the connection to the other side open throughout the communication and ensure the arrival of the data (bytes) in the sequence in which they were sent. Already the UDP sockets do not establish connection, only "play" a data packet in the network and do not guarantee that this data reaches the other side nor that its order is respected.

With sockets you can implement inter-process communication or even a higher-level protocol (called an application protocol, due to the layer it is located in ISO/OSI model) which other developers may use, such as HTTP itself or Websocket, implementing protocols implemented on TCP sockets.

Since HTTP follows a well-defined sequence of steps (opening the connection, requesting, receiving the response, and closing the connection), it usually gives the impression of not being persistent. But underneath the scenes he’s running these steps on persistent communication, allowed by TCP. There are techniques that keep the HTTP connection open for a long time for persistent data transmission (see Comet). Are "".

However, a more appropriate way to keep an open connection between Javascript browsers for real-time data exchange is the comparatively new Websocket protocol. It is an application protocol, also based on TCP, that can be implemented by web servers and served on the same port as HTTP (port 80). HTTP has been relatively well thought out and allows this "migration" from one application protocol to another, after a Handshake initial at HTTP.

It can be concluded that, despite the similarity of the names, they are different concepts. Websocket is an application-to-application communication protocol (browsers), which beneath the screens has its data carried by the TCP protocol, which in turn bridges the gap between two sockets (operating system concept). In other words, Websocket uses sockets.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.