Why are chat apps implemented using socket communication?

Seong-Am Kim
5 min readMay 31, 2021

Due to COVID-19, there has been a lot of non-face-to-face contact with people, and naturally there has been a lot of online communication.

Photo by Connor Olson on Unsplash

There are many apps and web related to chatting, but the big difference between other apps and the web is the communication.

There are many ways to implement chat, but this article focuses on implementing Socket communications.

1. Basic Concepts of Communication

Photo by Markus Spiske on Unsplash

As we already know, many apps and web services consist of servers and clients. So how do servers and clients communicate?

Each country has a promise of language when it comes to communicating with us as people. For example, I am Korean. Korean is what we promise each other and we use it to communicate with Koreans.

But I will experience inconvenience when I communicate with people from other countries who don’t know Korean. In order to communicate with each other like this, we have to make a promise with certain rules.

Like this, computers need appointments to communicate with each other. We call this a protocol.

The communication between the server and the client follows the TCP/IP protocol, so let’s take a closer look at it.

TCP (Transmission Control Protocol)

According to the technical writer Ben Lutkevich, TCP is described as follows.

TCP (Transmission Control Protocol) is a standard that defines how to establish and maintain a network conversation through which application programs can exchange data.

IP (Internet Protocol)

According to Wikipedia, Ip is described as follows.

The Internet Protocol (IP) is the principal communications protocol in the Internet protocol suite for relaying datagrams across network boundaries. Its routing function enables internetworking, and essentially establishes the Internet.

TCP/IP

It is often called TCP/IP because the two complement each other. The two concepts are quite similar, which is not easy to distinguish at first sight.

The differences between these two concepts are as follows.

TCP defines how applications can create communication channels across a network. IP defines the way each packet is addressed and routed to ensure it reaches the correct destination (EDUCABA).

Most of the communication we are using is within these two protocols.

2. What is a Socket communication?

Photo by israel palacio on Unsplash

https://www.buymeacoffee.com/jayflowhttps://www.buymeacoffee.com/jayflowOn this topic, we will compare the HTTP protocol and socket protocol commonly used on the web, and see why chat-related technologies are implemented using socket communication.

First, let’s talk about the HTTP protocol.

HTTP

In MDN, HTTP is described as follows.

HTTP is a protocol which allows the fetching of resources, such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser

MDN <An overview of HTTP>

What I want to emphasize in this description is that the response starts with the client.

Then how about connecting the socket?

Socket

Java documents describe Socket as follows.

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.

What’s important here is that it’s a two-way connection. The client requests the same as HTTP, but this is only a request to connect to the server, not a request to respond to data. Subsequently, the relationship between the server and the client maintains two-way communication between the server and the client unless otherwise requested to disable socket communication, which means that the server can send data to the client first if it connects to the client.

The difference between HTTP and socket communication

Based on the description of HTTP communication and socket communication, the differences between the two methods are shown below.

The difference between HTTP and socket communication is that a server can only respond to data about requests when requested by a client. However, in the case of socket communication, data can be sent to the client without request from the client after initial connection.

Why does the chat implementation use socket communication?

If you think about the difference between HTTP and socket communication, it naturally answers the problem.

The caveat here is that HTTP communication does not mean that requests and responses to connections occur once and end in a 1:1 response.

Generally, the above is true, but it can be implemented to ensure that the server-client connection is sustained.

<MDN — Connection management in HTTP/1.x>

Please refer to the link below for details on this.

Back to the main point, the reason for using socket communication for chatting is that HTTP communication can only respond when a client requests it, while Socket communication can respond to data from the client without a client request after the initial connection.

In general, for a chat, multiple clients are connected. During a conversation, data comes and goes between clients, and even if the client does not request it, the server first responds to the data enables efficient programming.

This is why we use socket communication when implementing chatting.

As I study, I wrote the corresponding article, so there may be some mistakes or deficiencies. If you give me your opinion on this at any time, I will reflect it.

Questions are always welcome! Thank you for reading the insufficient article.

--

--