What are WebSockets? How is it different from HTTP?

Mehul - Codedamn
25 Aug 202108:46

Summary

TLDRThis video script provides an insightful comparison between the HTTP and WebSocket protocols, two widely used internet protocols. The host, Mehul, begins by introducing HTTP, explaining how it establishes a TCP connection between the client and server to facilitate data transfer in a single direction. He then contrasts this with WebSockets, which enable bidirectional communication, allowing both client and server to exchange data seamlessly. The script highlights the stateless nature of HTTP, making it suitable for horizontal scaling, while WebSockets maintain a stateful connection, potentially limiting scalability. However, solutions like AWS API Gateway are mentioned as a way to address this issue. Overall, the script aims to help viewers understand the fundamental differences between these protocols and their respective strengths and use cases.

Takeaways

  • 📡 HTTP (Hypertext Transfer Protocol) is the primary protocol used for browsing the internet, including websites like Google and Facebook.
  • 📈 HTTP is stateless, meaning each request from a client to a server must include all necessary information, allowing for easier scalability and horizontal scaling.
  • 📯 TCP (Transmission Control Protocol) establishes a duplex (two-way) connection between the client and server, forming the basis for HTTP communication.
  • 💻 WebSockets provide a two-way communication channel over a single TCP connection, allowing for real-time data exchange between the client and server.
  • 📊 WebSockets start with an HTTP request to upgrade the connection, transitioning from a stateless to a stateful connection, enabling continuous data flow.
  • 🗣️ The duplex nature of WebSockets allows both the client and server to send data at any time, similar to a phone call, unlike the request-response model of HTTP.
  • 🔧 WebSockets are inherently faster and more efficient for real-time applications because they eliminate the overhead of repeatedly establishing connections.
  • 🛠 WebSockets face scalability challenges due to their long-lived, stateful connections, which bind clients to specific servers.
  • 📚 Solutions like AWS API Gateway offer ways to address scalability issues with WebSockets, enabling the use of multiple servers to manage connections.
  • 📌 Both HTTP and WebSockets have their pros and cons, and the choice between them depends on the specific needs of the application.

Q & A

  • What is the primary function of the HTTP protocol on the internet?

    -HTTP is used for browsing websites and transferring data over the internet, which includes accessing web pages that start with HTTP or HTTPS.

  • How does the TCP connection relate to HTTP communication?

    -After DNS resolution, the browser establishes a TCP connection between the user's computer and the server. This duplex connection is foundational for HTTP communication, facilitating the exchange of requests and responses.

  • Why is HTTP considered a stateless protocol?

    -HTTP is considered stateless because it doesn't maintain any information about previous requests. Each request is treated independently, requiring all necessary data to be sent with each request.

  • How does HTTP's stateless nature benefit scalability?

    -The stateless nature of HTTP simplifies horizontal scaling by allowing multiple servers to handle requests independently, without needing to maintain state information about clients.

  • What makes WebSockets different from HTTP?

    -WebSockets allow for a two-way communication channel between the client and server, akin to a phone call, enabling both parties to send messages at any time without establishing new connections for each exchange.

  • How are WebSockets initiated over the TCP/IP stack?

    -WebSockets start with an HTTP request to upgrade the connection to a WebSocket. Once the server agrees, the connection is upgraded, allowing for a persistent, two-way communication channel.

  • What are the implications of WebSockets being stateful?

    -Because WebSockets maintain a stateful connection, they do not inherently support horizontal scaling as easily as HTTP does. A single server must manage the connection for each client, which can limit scalability.

  • Why might HTTP be considered slower than WebSockets?

    -HTTP can be slower because each request and response includes overhead, such as HTTP headers, and requires establishing a new connection each time, whereas WebSockets maintain a continuous connection, reducing overhead.

  • How do WebSockets achieve scalability despite being stateful?

    -Solutions like AWS API Gateway help address scalability challenges with WebSockets by managing connections and distributing messages, thus enabling scalable real-time applications.

  • What is the significance of HTTP's request-response pattern?

    -HTTP's request-response pattern is designed for single-direction communication, where the client sends a request and the server responds, making it well-suited for traditional web applications.

Outlines

00:00

🌐 HTTP vs WebSocket: A Primer on Internet Protocols

In this video, the host introduces the topic of HTTP and WebSocket protocols, explaining how HTTP is used for browsing websites and how it establishes a TCP connection between the client and server. The client sends a GET request with headers, and the server responds with the requested document, closing the connection afterwards. HTTP is stateless, meaning the client must include all necessary headers like cookies and authentication for each request. This statelessness enables horizontal scaling with multiple servers handling requests. In contrast, WebSockets establish a persistent, dual-channel connection upgraded from an initial HTTP request. This allows bidirectional communication, similar to a phone call, but sacrifices the scalability advantages of stateless HTTP.

05:02

⚖️ The Trade-offs: WebSocket Scalability and Performance

The video continues by contrasting the differences between HTTP and WebSockets. While HTTP is like sending a letter and closing the connection after each transaction, WebSockets maintain a stateful, long-running connection akin to a phone call. This statefulness means the client doesn't need to re-authenticate for every message, but it also limits horizontal scalability as a single server must handle all messages from a client. However, solutions like AWS API Gateway can help address scaling issues with WebSockets. The video also notes that WebSockets are generally faster than HTTP since they don't transfer as much data per message. Ultimately, the choice depends on the specific use case: HTTP is stateless and scales better, while WebSockets offer persistent, low-latency communication at the cost of reduced scalability.

Mindmap

Keywords

💡HTTP

HTTP (Hypertext Transfer Protocol) is the protocol used for browsing websites like Google, Facebook, and Codedam on the internet. It is a stateless protocol, meaning that every request from the client (browser) must include all necessary headers and authentication details. In the video, HTTP is described as establishing a TCP connection between the client and server, sending a request (e.g., GET /abc), and receiving a response document from the server before closing the connection.

💡WebSocket

WebSocket is a communication protocol that allows for a two-way, stateful connection between a client and server. Unlike HTTP, where the connection is closed after each request-response cycle, a WebSocket connection remains open, enabling real-time, bidirectional communication. The video compares a WebSocket connection to a phone call, where both parties can send and receive messages simultaneously without re-establishing the connection each time.

💡TCP

TCP (Transmission Control Protocol) is a Layer 4 protocol in the OSI model, responsible for establishing reliable connections between devices on a network. Both HTTP and WebSocket connections rely on TCP for establishing and maintaining connections between clients and servers. The video explains that TCP connections are duplex, meaning that data can flow in both directions simultaneously.

💡Stateless

A stateless protocol, like HTTP, does not maintain any information about previous requests or connections. Each request from the client must include all necessary headers and authentication details, as the server treats each request as an isolated transaction. This stateless nature of HTTP simplifies horizontal scaling, as multiple servers can handle requests independently without needing to maintain state information.

💡Stateful

A stateful protocol, like WebSocket, maintains information about the connection and its state over time. Once a WebSocket connection is established, subsequent messages do not need to include authentication or other headers, as the server can maintain the state of the connection. This stateful nature makes WebSocket connections more challenging to scale horizontally, as a single server must handle all messages from a particular client.

💡Scalability

Scalability refers to the ability of a system to handle increasing workloads or user traffic. The video contrasts the scalability of HTTP and WebSocket protocols. HTTP's stateless nature makes it easier to scale horizontally by adding more servers to handle individual requests independently. WebSocket connections, being stateful, are more challenging to scale horizontally, as a single server must handle all messages from a particular client.

💡Horizontal Scaling

Horizontal scaling is the process of adding more servers or instances to a system to handle increased workload or traffic. The video highlights that HTTP's stateless nature facilitates horizontal scaling, as multiple servers can handle individual requests independently without needing to maintain state information. In contrast, WebSocket connections, being stateful, make horizontal scaling more challenging, as a single server must handle all messages from a particular client.

💡Real-time Communication

Real-time communication refers to the ability to transmit and receive data or messages with minimal delay or latency. The video emphasizes that WebSocket connections are well-suited for real-time communication, as they maintain a persistent, bidirectional connection between the client and server, allowing for efficient exchange of messages without the overhead of re-establishing connections for each message.

💡DNS Resolution

DNS (Domain Name System) resolution is the process of translating a human-readable domain name, like google.com, into an IP address that can be used for network communication. The video briefly mentions that DNS resolution occurs before the HTTP connection is established, as the client needs to determine the IP address of the server it wants to connect to.

💡Headers

Headers are metadata included in HTTP requests and responses, providing additional information about the request or response. The video mentions that in HTTP, the client must include headers like cookies, authentication details, and other relevant information with each request, as HTTP is a stateless protocol. This overhead is reduced in WebSocket connections, as the headers are only required during the initial handshake phase.

Highlights

HTTP is the protocol used for browsing websites, starting with http or https.

When you enter a website URL, the browser establishes a TCP connection with the server after DNS resolution.

HTTP communication is one-way, with either the client sending a request or the server responding.

HTTP is stateless, requiring the client to specify headers like cookies and authentication for each request.

The stateless nature of HTTP simplifies horizontal scaling by allowing multiple servers to handle requests.

WebSockets operate like TCP, with dual communication between the client and server.

WebSockets establish a long-running, stateful connection after an initial HTTP upgrade request.

WebSockets allow both the client and server to send messages at any time, unlike the one-way nature of HTTP.

The stateful connection in WebSockets makes horizontal scaling more challenging as a single server handles a client.

Solutions like AWS API Gateway can address scalability issues with WebSockets.

HTTP is slower than WebSockets due to the overhead of transferring data with each request.

WebSockets are faster due to the long-living connection, but HTTP and WebSockets have different use cases.

The choice between HTTP and WebSockets depends on the specific requirements of the application.

The video aims to provide an understanding of the differences between HTTP and WebSockets.

The video encourages viewers to like, subscribe, and provide feedback in the comments.

Transcripts

play00:00

hey everyone welcome back my name is

play00:01

mehul and in this video we'll be taking

play00:03

a look at http vs websocket very two

play00:07

popular protocols which you use on the

play00:09

internet all the time without realizing

play00:12

also if you're new here make sure you

play00:13

like and subscribe to the channel i'm

play00:15

creating a lot of interactive and

play00:16

awesome content here at goddamn and even

play00:18

at codedam.com platform so if you want

play00:21

to stay in touch make sure you subscribe

play00:23

and hit the bell icon let's go so http

play00:25

is the protocol which you always use

play00:27

when you're browsing websites like

play00:29

google facebook code damn code dumps

play00:32

learning path and basically anything

play00:34

which you browse on the internet which

play00:35

starts with http or https that is done

play00:38

using http protocol so let's see what

play00:40

exactly happens when you write

play00:42

google.com and press enter

play00:45

on the http front it's not on the dns

play00:47

front on the http front so

play00:49

when you write http google.com what

play00:52

happens is that the first thing which

play00:54

your browser does is that it establishes

play00:57

after the dns resolution and the ip

play00:58

address has been figured out is that it

play01:00

establishes a tcp connection between

play01:02

your computer which you have here and

play01:05

the server which is owned by google

play01:08

right so this tcp connection is also

play01:11

called as layer 4 protocol the tcp

play01:13

protocol is called layer 4 in the osi

play01:15

model and these are just some layers of

play01:18

how the networking stack works physical

play01:20

layer then you know it goes on top and

play01:23

top and there are more and more

play01:24

abstractions we are not going to get

play01:25

into that but what you have to

play01:27

understand is that this connection is

play01:29

duplex so tcp connection is duplex and

play01:32

after some handshakes once it's

play01:34

established then what http does is that

play01:38

http in this case

play01:40

your computer right here

play01:43

says the standard things which you see

play01:45

get

play01:46

slash and you know this is the path for

play01:48

example get slash abc and then the http

play01:52

headers and this is sent to your server

play01:54

your server receives this part inside of

play01:57

tcp packet and responds with the correct

play02:01

document and that is it that is all http

play02:04

does right it just does this part

play02:06

particular communication and then closes

play02:08

off so what http does is that it only

play02:10

allows communication in a single

play02:12

direction so either the client speaks

play02:14

and the server response or the server

play02:16

speaks and the client response tcp on

play02:18

the other hand is inherently a two-way

play02:20

communication where both client and

play02:22

server can speak at the same time now

play02:24

you might think that why is that

play02:25

limitation for http it seems like a

play02:28

limitation right if you're going up the

play02:29

stack and you're kind of like losing the

play02:31

access to the to the functionality of

play02:34

both the people talking to each other

play02:36

there's a good reason for this this is

play02:38

because http as a protocol is stateless

play02:41

so this protocol is stateless that means

play02:44

every single time every single time

play02:47

if client wants to send something to the

play02:50

server the client has to specify all the

play02:52

headers that is the cookie header the

play02:54

authentication header whatever headers

play02:56

it wants and it has to send all of them

play02:58

to the server why this is a good thing

play03:00

because this allows http to stay

play03:02

stateless that means you can have

play03:04

multiple servers at the back end

play03:07

handling different requests right so it

play03:09

simplifies your architecture of scaling

play03:11

a lot horizontal scaling a lot right

play03:13

because now you can have a million

play03:16

clients here requesting the same page

play03:18

and this all the backend has to do is

play03:20

add more servers in order to serve these

play03:22

individual requests why because after

play03:25

every single http request that http

play03:28

connection gets closed tcp connection is

play03:31

open if you have a keeper live header

play03:32

but let's just assume for the sake of

play03:34

simplicity that this connection gets

play03:36

closed and the next time the next

play03:37

request arrives the next request is made

play03:39

by the client this connection

play03:41

might establish to some other server

play03:43

right and because this is stateless the

play03:45

other server exactly knows what the

play03:47

client wants websockets however behave

play03:49

in a slightly different way because they

play03:52

actually operate much more like

play03:54

how tcp works in terms of dual

play03:57

communication so of course websockets

play04:00

also works on tcp layer 4 communication

play04:03

layer 4 protocol and on top of this

play04:06

sometimes what you will see is that the

play04:08

initial request which the server sends

play04:10

is an http request to a websocket

play04:13

endpoint the client sends this request

play04:15

the server says that hey

play04:16

we can have a connection

play04:19

upgraded to websocket it also asks like

play04:21

can the connection be upgraded to

play04:23

websocket it says yep i can upgrade that

play04:26

connection to websocket and once that is

play04:28

done this connection is it's not exactly

play04:30

dropped but it's upgraded to our

play04:32

websocket connection and this websocket

play04:35

connection right here is awesome why

play04:37

because this actually is a dual channel

play04:40

established between the client and the

play04:42

server so how you can imagine this you

play04:44

can imagine this in the following way

play04:46

that this

play04:47

has

play04:48

actually called you know i'm just trying

play04:50

to draw a simple mobile phone this has

play04:52

actually called this

play04:54

server right here so both of them are

play04:57

now on a phone call where they pretty

play04:59

much can say whatever they want to each

play05:01

other

play05:02

any time of the day how does this differ

play05:05

from pc uh from your http that differs

play05:09

in the sense that your http connection

play05:12

is more like sending a letter which

play05:14

probably the symbols would for later

play05:16

also would seem seem the same but i'm

play05:18

just writing an l here so you send a

play05:20

letter once then the server reads it

play05:23

then the server responds with another

play05:25

letter and that's it the connection then

play05:27

closes right for the next time the

play05:29

client has to again initiate the

play05:31

connection or the server can as well if

play05:33

it knows the ip then in that case server

play05:35

becomes the client but you get the idea

play05:37

so this is just like a one-time

play05:39

transaction which is happening here but

play05:40

a major fault which you will observe

play05:42

here is that we lose that scalability

play05:44

aspect which we talked in the last video

play05:47

right because now if this client starts

play05:50

sending a thousand messages then only

play05:53

this server has to handle it why because

play05:56

they are on a phone call right so it

play05:58

cannot really drop the connection

play06:00

because if it do that then it pretty

play06:03

much will

play06:04

you know just drop the connection here

play06:06

and the client will see that hey the

play06:08

connection has been lost and the state

play06:10

has been lost for the most part because

play06:12

because this is a single connection now

play06:14

this connection is stateful that means

play06:17

it's not that on every single message

play06:20

this client has to tell who the client

play06:22

is right that authentication phase only

play06:24

happens when there is a handshake going

play06:27

on for the very first time after the

play06:29

handshake is complete

play06:31

this is a stateful connection to put

play06:33

this thing in another way your client

play06:36

establishes a tcp connection once that

play06:38

is established then your client

play06:41

establishes an http connection and tries

play06:44

to upgrade the socket to upgrade the

play06:46

connection to websocket once that has

play06:48

happened then this connection is

play06:51

upgraded to a stateful connection right

play06:54

here right now it can send some messages

play06:56

it can send some messages at any single

play06:58

time this has the drawback that you

play07:00

cannot use multiple servers to handle

play07:02

multiple messages because this channel

play07:04

is already established and it's long

play07:06

running right this problem does not

play07:09

happen in case of http because you can

play07:10

have multiple servers and all it is

play07:13

doing is just handling this one single

play07:15

request and that's it then it's closed

play07:17

then for the next request another

play07:19

channel opens up right and then that

play07:20

happens so it seems like websockets is

play07:23

fundamentally not horizontally scalable

play07:25

because you will always have this

play07:26

problem where a server is stuck with a

play07:29

client right and that is partly true but

play07:32

there are very interesting solutions

play07:33

like aws api gateway which allows you to

play07:36

address certain problems and we will

play07:38

definitely see how we can scale

play07:40

websocket connections in some further

play07:42

videos but i hope this gave you an

play07:43

understanding of how websockets differ

play07:45

from http there are some obvious

play07:48

differences as well for example http is

play07:50

definitely slower because you are

play07:51

transferring a lot more data in every

play07:53

single request compared to websockets

play07:55

websockets are long living therefore

play07:58

they are obviously faster as well

play08:00

because it's like on a phone call this

play08:02

is more like

play08:03

sending a letter or maybe like you know

play08:05

just giving a ring and then or sending a

play08:07

text message for example but yeah i mean

play08:09

both have their own pros and cons right

play08:11

so it's it's not like one is perfect for

play08:14

a single job it depends on what you want

play08:16

to do so yeah that's pretty much it for

play08:18

websockets versus http let me know in

play08:20

the comments what you think about this

play08:22

video and this video format in general i

play08:24

hope this video was able to provide you

play08:26

some value if it did make sure you like

play08:28

and subscribe thank you so much for

play08:29

watching and i'm gonna see you in the

play08:31

next video

play08:33

[Music]

play08:45

you

Rate This

5.0 / 5 (0 votes)

Related Tags
Internet ProtocolsHTTP vs WebSocketTCP ConnectionWeb TechnologyNetworking FundamentalsStateless ProtocolDuplex CommunicationWeb DevelopmentScalability SolutionsAPI Gateway