Exploring WebSocket And Its Brief Implementation for Android

Rounak Modi
Walmart Global Tech Blog
5 min readNov 12, 2021
Photo by Kenny Leys on Unsplash
Source

Imagine you want to talk about something urgent with your friend, so you write a letter, post it and then wait for their reply for days rather than calling or texting them on a messaging app. Sounds like the old school way right? We face problems in life that require immediate solutions and in the present time, we want technology to enable the real-time flow of information to solve these problems. The traditional HTTP protocol is based on the request-response model which can be a hindrance and cause delays in real-time communications. This is where real-time communication protocol comes to the rescue. In this article, we will be exploring WebSocket as a real-time communication protocol and its implementation for Android Client. You can find more real-time communication protocols here.

What is WebSocket?

Let’s begin with a brief history of WebSocket. Discussion on WebSocket protocol started in the year 2008 and it was standardised as a protocol by IETF as RFC 6455 in 2011. It was referenced for the first time in HTML5 as TCP-based socket API.

Wikipedia defines WebSocket as :

WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.

I can explain this definition to you with a simplified analogy. Suppose you again want to talk about something urgent with your friend and this time you workaround in a wise way and decide to call them. As soon as you dial their number then a request is sent to them, when they pick up the call a connection is established between you and them. Now you and your friend can talk for as long as the pair desire. The connection remains established unless anyone closes the connection, in our case hangs up the phone call. WebSocket protocol is bidirectional, duplex and sustains on a single TCP connection. Now that you have accomplished the WebSocket understanding, you might want to find out the list of WebSocket clients. Well, I can simplify this. Here is a list of browsers that support WebSockets.

HTTP vs WebSocket Protocol

HTTP stands for Hypertext Transfer Protocol. It is an application layer protocol and is widely used for data communication by the World Wide Web. The endpoint for the HTTP server is either represented as http:// or https://. In the client-server communication model, it acts as a request-response protocol. HTTP is a unidirectional protocol. Firstly the connection between a client and server is successfully established and then the client sends a request message to the server. The server receives the message, processes it and responds back to the client with a success or error code. Finally, the connection is closed. For each request or say for 100 requests a new connection or 100 new connections are established and then closed.

HTTP client-server connection

WebSocket is a bidirectional protocol. The WebSocket endpoint is represented as ws:// or wss://. Client and server over a WebSocket connection remain connected unless it is terminated by either (remember the phone call analogy!!). Once a successful connection between the client and server is established, either of them can send data being independent of each other which continues unless either of the parties closes the connection. Now for 100 messages, once a new connection between client and server is established, they can send 100 messages to each other and then the connection gets closed.

WebSocket client-server connection

WebSocket Use-case :

WebSocket makes event-driven programming easier, it allows the client or a server to send the message. It can be used for an application that requires a real-time update, some of such applications are :

  1. Chat / Chatbot applications.
  2. Game applications.
  3. Stock applications.
  4. Multi-User applications.
  5. Sports applications.
  6. News applications.
  7. Location services.

For some of the above use cases, HTTP might cause unnecessary delays and fail to scale.

WebSocket Implementation For Android :

ENIAC: First Electronic Programmable Computer

Over the past few decades, the way electronic machines have revolutionised is a matter of mere astonishment. There was an era when large spaces were needed to store a computer system, while in present days we have android phones that can be easily held in our hands. Android phones have been no less than computer machines in providing access to various Web Applications via Android Applications. I have discussed enough of the wonders made successful by Android phones. This section will exchange some information on implementing WebSocket for Android clients. If you want to learn and explore Android beyond use this link.

OkHttp is an open-source library developed by Square. One of the options to develop a WebSocket android client is using OkHttp3. It has a WebSocketListener class that implements all required methods for creating a WebSocket connection, sending a message, handling errors and finally closing a WebSocket connection. More details on the class and its methods are available here.

Takeaways :

Demand for speed and ease of access is becoming ever more important. Android application with WebSocket might be a good way to achieve it. HTTP and WebSocket have a different use case. As developers, we need to analyse the solution which is best for our use case. Coming to the end of this article, I hope you will try implementing WebSocket. It might seem difficult at first but as Nelson Mandela said “It always seems impossible until it’s done”.

--

--