Open In App

What is the use of WebSocket API ?

Improve
Improve
Like Article
Like
Save
Share
Report

API stands for Application Programming Interface. API can be defined as a predefined set of instructions that describes how different applications can communicate with one another. So one can think of API as a middleman for transferring data between a web server and the application. Every app is using some kind of API to transfer data between applications. API provides the ability for different applications to open up their functionalities and data to other external parties to make use of it and develop new applications. Google Maps API and Twitter API are some well-known examples.

WebSocket: WebSocket is a communication protocol that is mainly used for communication between a client and server.

Features of WebSocket are:

  • Full-Duplex Protocol: WebSocket is a full-duplex protocol as it allows the application to send and receive data at the same time.
  • Stateful Protocol: It means the connection between server and client will not be terminated until and unless closed by any one of them either by the client or by the server. Once the connection is terminated from one end it is also closed by another end.
  • 3-way handshake: Websocket uses a 3-way handshake also known as TCP connection for establishing communication between a client and server.

WebSocket API: WebSocket API allows us to create web sockets, it is a javascript API that is capable of full-duplex communication using a TCP connection. WebSocket uses port 80 by default.

Features of WebSocket API are:

  • Bidirectional means data can be sent and received by both sides client-side as well as the server-side.
  • Use of full-duplex model for communication.
  • It uses a single TCP connection for communication between the client and server.
  • Mainly used in real-time applications like chat applications, video calls applications, etc.
  • Fast transmission of data can be achieved using web sockets.
  • Scaling is possible but only vertically.

Use of WebSocket API: There is a wide range of uses of WebSocket API’s some of them are:

  • Online Education Sector: Web sockets are obviously used in online education applications because these applications need real-time data for video streaming or for sharing screen which makes web sockets a very good choice because it has the capability to provide all these functionalities.
  • Gaming: The world is going crazy after games, users want real-time games to play with multiple players along with chat and call facilities so to achieve all these things it is a must for anyone to use web sockets for developing different gaming applications.
  • Collaborative applications: We all have used google docs which makes it possible for multiple persons to use the same workspace and work concurrently, these applications are built using HTML5 WebSockets.
  • Real-time data visualization: Visualisation of real-time data in an appealing manner was a quite tough task earlier but with the use of html5 web sockets it is really easy.
  • Event Update applications: Web sockets are extensively used in making applications for giving real-time updates of all platforms to some common platform.
  • Tracking User Behaviour: Organisations are really interested in knowing users’ behavior while interacting with a web application so to give better recommendations about the content or product the user is looking for. In achieving all these things web socket is a must-go choice.

Websocket Implementation Using Python Fastapi:

Python3




from fastapi import FastAPI, WebSocket
 
app = FastAPI()
 
@app.websocket("/ws")
async def websocket_endpoint(webs: WebSocket):
      await webs.accept()
    while True:
      raw_text = await webs.receive_text()
      await webs.send_text(f'received data {raw_text}')
       
       
  # To test the code use, ws://localhost:8000/ws
  #in postman



Last Updated : 10 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads