Websockets VS HTTP

In this article we are going to learn differences between Websockets and HTTP. Before we move forward, we should have knowledge about following terminology.

  • Network Protocol: It is a set of rules and regulations. For example: IP, ICMP, FTP, UDP etc…
  • Network Socket: It uses to connecting two nodes (machines) over network to transmit data by the help of IP and TCP port. This communication may established between two different systems. It mainly use for client-server environment (point-to-point connection). Combination of IP and Port is called socket. Socket work as API.
  • Network PORT: Every computer or machine on network has own unique IP address. You can access this machine through IP but can’t access to programs (applications) running on to this machine. So, we assign the port number to access these programs. Some port numbers (0-1023) are for specific purposes. IP and port number makes the end-point. It does matter what is the port number.
  • TCP: Transmission Control Protocol is used to establish and maintain the network connection and It guarantees the delivery of each package. It is in transport layer of OSI.
  • UDP: User Data-gram Protocol is used to stream the data. It is a connection less protocol.
  • One-way Communication Example (Uni-directional): HTTP is one-way communication (client to server)
  • Two-way Communication Example (Bi-directional): Web-socket is two-way communication. So, through web-socket HTTP can be bi-directional.
  • HTTP Polling:
    • Short-Polling: The client sends a request to the server at specified time interval.
    • Long-Polling: The server keeps the connection open and respond to client only new data available at specified time interval.
  • Real-time: Two-way communication between two machines over network is called real-time. Chat applications, online gaming, firebase, google docs etc… are the examples of real time application.
  • Streaming: Client sends the request once and then server responds consistently.

HTTP:

  • HTTP is internet protocol to transfer the data between client and server.
  • HTTP uses TCP to transmit the data.
  • It present in the application layer of OSI.
  • It is one-way communication between client and server.
    • Client send request to server (with session id).
    • Server response back to the client (against client session id).
    • After the response the connection closes.
  • HTTP is stateless communication.

Websocket (ws://, wss://):

  • Websocket is extension of HTTP for browser based applications.
  • It uses HTTP only to handshake for opening and closing connection.
  • It will convert into 2-way communication over single TCP connection after server respond HTTP 101 (upgrade the header). Data is transferred in the form of frames. Data can be encrypted or unencrypted. Connection is always open until send the close request.
  • Web socket is alternative of short or long polling.
  • Web socket technologies
    • Socket.io,
    • Meteor.js
    • Firebase
    • signalR