How Web Sockets work | Deep Dive

ByteMonk
13 Apr 202410:21

Summary

TLDRThis video dives into advanced WebSocket functionalities, essential for building powerful real-time applications. It revisits the WebSocket handshake, detailing the negotiation process from HTTP to WebSocket, and explains the crucial headers exchanged. The video also explores the structure of WebSocket frames, including the importance of masking and fragmentation for secure and efficient data transmission. Applications in various domains, such as chat systems, web applications, gaming, and stock exchanges, demonstrate the versatility and potential of WebSockets for real-time communication.

Takeaways

  • ๐ŸŒ Websockets enable real-time two-way communication between browsers and servers, which is more efficient than traditional HTTP requests that require a new connection for each request.
  • ๐Ÿค The WebSocket handshake is a crucial step where the client and server negotiate the upgrade from HTTP to a WebSocket connection, involving specific headers and a handshake process.
  • ๐Ÿ”‘ The 'Sec-WebSocket-Key' header is a randomly generated base64-encoded string used by the server to verify the handshake, ensuring a secure and authenticated connection.
  • ๐Ÿ—‚๏ธ The 'Sec-WebSocket-Accept' header is generated by the server using a hash function and the client's 'Sec-WebSocket-Key', proving that the server recognizes the handshake initiation.
  • ๐Ÿ”„ Masking in WebSockets obscures payload data to prevent it from being interpreted as HTTP content by caching proxies, ensuring smooth data transition through network infrastructure.
  • ๐Ÿ“ฆ Frames are the basic units of data exchanged in WebSockets, with each frame having a specific structure including fin bits, opcode, mask bit, payload length, and payload data.
  • ๐Ÿ”„ Fragmentation in WebSockets refers to splitting a large message into smaller chunks for transmission, which is useful for handling large messages without overwhelming the connection.
  • ๐Ÿ“ˆ The FIN bit in a WebSocket frame indicates whether the frame is the final fragment of a message or if more fragments are to follow, allowing for gradual delivery of large messages.
  • ๐Ÿ”’ WSS (WebSocket Secure) is used for secure communication over TLS, ensuring encrypted data transmission between the client and server.
  • ๐ŸŽฎ WebSockets are versatile and find applications in various domains such as chat applications, real-time updates in web applications, multiplayer gaming, and real-time stock ticker updates.
  • ๐Ÿ’ก Understanding the advanced concepts of WebSockets, such as fragmentation and masking, is key to mastering real-time communication technologies and unlocking their full potential.

Q & A

  • What is the primary purpose of the WebSocket handshake?

    -The primary purpose of the WebSocket handshake is to establish a real-time communication channel between a client (browser) and a server by upgrading from an HTTP connection to a WebSocket connection.

  • Why is the WebSocket protocol considered to be more efficient than traditional HTTP for real-time communication?

    -WebSocket protocol is more efficient for real-time communication because it uses a persistent connection that remains open as long as it is not interrupted by either party, unlike traditional HTTP which creates a new connection for each request and closes it after the request is completed.

  • What does the 'Sec-WebSocket-Key' header in a WebSocket handshake request contain?

    -The 'Sec-WebSocket-Key' header contains a randomly generated base64-encoded string that is unique to the handshake. It is used by the server to verify the handshake later.

  • What HTTP status code indicates a successful WebSocket handshake upgrade?

    -A successful WebSocket handshake upgrade is indicated by an HTTP status code of 101, which stands for 'Switching Protocols'.

  • How does the server verify the WebSocket handshake request from the client?

    -The server verifies the WebSocket handshake request by checking the presence of 'Upgrade: websocket' and 'Connection: Upgrade' headers, and then generates a 'Sec-WebSocket-Accept' header using a cryptographic hash function applied to the 'Sec-WebSocket-Key' from the client, followed by a base64 encoding.

  • What is the role of masking in WebSocket communication?

    -Masking in WebSocket communication ensures that the data within the frames is unpredictable and does not resemble valid HTTP requests or responses, which discourages intermediaries from caching or modifying the data.

  • Why is WebSocket fragmentation important for handling large messages?

    -WebSocket fragmentation is important for handling large messages because it splits a large message into smaller chunks, preventing buffer overflow and allowing for gradual delivery of the message, which is beneficial for applications where immediate display of partial data is valuable.

  • What does the 'Fin' bit in a WebSocket frame indicate?

    -The 'Fin' bit in a WebSocket frame indicates whether the frame is the final fragment of a message or if there are more fragments to come. A value of one means it's the final fragment, while a value of zero means there are more fragments.

  • What are some applications of WebSocket technology?

    -WebSocket technology is used in various applications such as chat applications for real-time messaging, web applications for live updates, multiplayer gaming for seamless experiences, real-time drawing apps for collaboration, and stock exchanges for instant updates on stock prices.

  • How does the WebSocket protocol handle secure communication?

    -The WebSocket protocol handles secure communication by using 'wss://' as the scheme, which sets a secure flag and initiates a TLS handshake between the server and client for encrypted communication.

  • What are the reserved bits 'RSV1', 'RSV2', and 'RSV3' in a WebSocket frame used for?

    -The reserved bits 'RSV1', 'RSV2', and 'RSV3' in a WebSocket frame are typically set to zero and are reserved for future use cases or potential extensions to the WebSocket protocol.

Outlines

00:00

๐ŸŒ Introduction to Advanced WebSocket Functionalities

The video begins with a recap of basic WebSocket concepts and introduces the topic of advanced functionalities. The focus is on understanding the WebSocket handshake, which upgrades HTTP to WebSocket for real-time communication. This section details the steps and headers involved in the handshake process, highlighting the differences between HTTP and WebSocket connections.

05:00

๐Ÿ”„ WebSocket Handshake Process

The second part dives into the specifics of the WebSocket handshake process. It explains how the client initiates a handshake with an HTTP GET request, including special headers. The server's response involves a status code of 101, confirming the upgrade. Key headers like 'Upgrade', 'Connection', and 'Sec-WebSocket-Key' are discussed in detail, along with the server's verification process using SHA-1 hashing and Base64 encoding.

10:02

๐Ÿ”’ Ensuring Secure and Authenticated Connections

This section emphasizes the importance of the WebSocket handshake in establishing a secure and authenticated connection. The process includes a random 'Sec-WebSocket-Key' from the client and a corresponding 'Sec-WebSocket-Accept' from the server, ensuring that the handshake is legitimate and secure. If any validation fails, the connection is not established.

๐Ÿ“ก WebSocket URI and Data Frames

Here, the video explains the format of WebSocket URIs (WS for insecure, WSS for secure connections) and the structure of data frames. Each frame includes fields like FIN, RSV, Opcode, Mask, Payload length, and Masking key, which are crucial for proper data transmission. The role of masking and fragmentation in ensuring smooth data flow and preventing issues with network infrastructure is also covered.

๐Ÿ“ฆ Handling Large Messages with Fragmentation

This part discusses the concept of fragmentation in WebSockets, which allows large messages to be split into smaller fragments. This prevents buffer overflow and enables gradual data delivery. The video explains the significance of the FIN bit in indicating the final frame of a message and how fragmented messages are processed for efficient data transmission.

๐ŸŽฎ Real-World Applications of WebSockets

The final section explores various real-world applications of WebSockets, such as chat applications, real-time web updates, multiplayer gaming, collaborative tools, and stock ticker updates. It encourages viewers to experiment and understand the underlying concepts for mastery, inviting questions and project ideas to foster community growth.

Mindmap

Keywords

๐Ÿ’กWebSockets

WebSockets is a communication protocol that provides full-duplex communication channels over a single TCP connection. It allows for real-time two-way interaction between a client (typically a web browser) and a server. In the video, WebSockets are discussed as a means to enable advanced real-time applications, emphasizing its role in reducing server load compared to traditional HTTP requests which require a new connection for each request.

๐Ÿ’กHandshake

The term 'handshake' in the context of the video refers to the initial process where the client and server agree to switch from an HTTP connection to a WebSocket connection. This is a crucial step as it sets up the persistent connection that is characteristic of WebSockets. The script details the headers exchanged and the steps involved in this process, highlighting its importance for establishing real-time communication.

๐Ÿ’กUpgrade Request

An 'upgrade request' is an HTTP header sent by the client indicating the desire to switch to a different protocol, in this case, WebSockets. The script explains that the client sends an HTTP GET request with specific headers, including 'Upgrade: websocket', to initiate the handshake process. This request is part of the negotiation between the client and server to establish a WebSocket connection.

๐Ÿ’กWebSocket Key

The 'WebSocket Key' is a base64-encoded random string sent by the client in the handshake process. It is used by the server to verify the handshake and ensure that the connection is being initiated by a legitimate WebSocket client. The script mentions that this key is crucial for the server's acceptance of the handshake, contributing to the security of the connection.

๐Ÿ’กSec-WebSocket-Accept

The 'Sec-WebSocket-Accept' header is part of the server's response to the client's handshake request. It contains a cryptographic hash of the 'WebSocket Key' provided by the client, demonstrating that the server has received and accepted the request to upgrade to a WebSocket connection. The script describes how this header confirms the successful establishment of the WebSocket connection.

๐Ÿ’กFrames

In WebSockets, 'frames' are the basic units of data exchanged between the client and server. Each frame has a specific structure that includes information about the type of data it carries, such as text, binary, or control frames like ping. The script explains that frames are used to transmit data in a structured way, allowing for efficient communication over the WebSocket connection.

๐Ÿ’กMasking

Masking in WebSockets is the process of obscuring the payload data to ensure that it does not resemble HTTP traffic, which can prevent issues with caching proxies that might otherwise interfere with the WebSocket data. The script describes how masking makes the data unpredictable, discouraging incorrect interpretation or manipulation by intermediaries.

๐Ÿ’กFragmentation

Fragmentation in the context of WebSockets refers to the division of a large message into smaller frames for transmission. This is useful for handling large messages that might exceed buffer limitations or cause performance issues. The script explains that fragmentation allows for the gradual delivery of large messages, improving the efficiency and reliability of data transfer over the WebSocket connection.

๐Ÿ’กFIN Bit

The 'FIN bit' in a WebSocket frame indicates whether the frame is the final fragment of a message or if more fragments are to follow. A value of one signifies the final fragment, while a value of zero indicates that additional fragments are expected. The script uses the FIN bit as an example to illustrate how messages are broken down and reassembled during fragmentation.

๐Ÿ’กReal-Time Applications

The term 'real-time applications' refers to software systems that require immediate responses or updates, such as chat applications, live gaming, or stock ticker updates. The script discusses how WebSockets enable the development of such applications by providing a persistent connection for continuous data exchange without the overhead of repeated HTTP requests and responses.

๐Ÿ’กWSS

WSS stands for WebSocket Secure and is used for establishing a secure WebSocket connection over TLS (Transport Layer Security). It is analogous to HTTPS for HTTP connections. The script mentions WSS as an example of how WebSockets can provide secure communication channels, important for applications that require data privacy and integrity.

Highlights

Introduction to advanced WebSocket functionalities for building powerful real-time applications.

WebSockets enable real-time two-way communication between browsers and servers, unlike traditional HTTP.

The WebSocket handshake is a crucial step for upgrading from HTTP to a WebSocket connection.

Explanation of the client and server steps involved in the WebSocket handshake process.

The WebSocket protocol is independent, with its only relationship to HTTP being the handshake.

Details on the HTTP GET request with specific headers for initiating a WebSocket upgrade.

The role of the 'Sec-WebSocket-Key' header in the handshake for server verification.

Server response with a status code of 101 indicates a successful upgrade to WebSocket.

The 'Sec-WebSocket-Accept' header verifies the client's handshake initiation to the server.

WebSocket connections are persistent and do not require constant HTTP requests and responses.

WebSocket frames are the basic units of data exchanged between client and server.

The structure of WebSocket frames includes FIN, RSV, Opcode, Mask, Payload Length, and Masking Key.

Masking ensures a smooth transition of real-time data through various network infrastructures.

Fragmentation in WebSockets allows for the transmission of large messages in smaller chunks.

FIN bit in fragments indicates whether it is the final frame or if more follow.

WebSockets' versatility in applications such as chat apps, web applications, gaming, and stock exchanges.

Emphasis on the importance of understanding WebSocket concepts for true mastery in real-time communication.

Transcripts

play00:00

hey everyone welcome back previously I

play00:01

have explained the basics of websockets

play00:03

and how they enable realtime two-way

play00:05

communication between browsers and

play00:06

servers but many of you wanted more so

play00:09

today we'll dive deeper and explore

play00:11

Advanced websocket functionalities and

play00:13

how to build powerful real-time

play00:15

applications with them we'll first

play00:17

revisit the websocket handshake which is

play00:19

a crucial step where the client and

play00:21

server negotiate the upgrade from HTTP

play00:23

to a websocket connection we'll then

play00:25

break down the handshake process in

play00:26

detail exploring the headers exchanged

play00:28

and the steps involved on both sides so

play00:31

let's dive in and get

play00:33

[Music]

play00:35

started previously in my web socket

play00:38

introduction video we learned that HTTP

play00:39

uses distinct connection for separate

play00:41

request it increases the load on the

play00:43

server as the server has to create a new

play00:45

handshake for every request once the

play00:47

request is completed connection is

play00:49

closed on the other hand websocket

play00:52

connection is persistent as long as it

play00:54

is not interrupted by either of the

play00:56

parties the websocket protocol is an

play00:58

independent TCP based protocol call its

play01:00

only relationship to http is that its

play01:03

handshake is interpreted by HTTP servers

play01:05

as an upgrade request the websocket

play01:08

handshake process is a crucial step that

play01:10

establishes a real-time Communication

play01:12

channel between a client which is

play01:13

browser and a server it leverages the

play01:16

familiar HTTP protocol for initial

play01:18

connection but then negotiates an

play01:20

upgrade to the websocket protocol here

play01:23

is a detail breakdown of the client and

play01:25

server steps involved the client opens a

play01:28

regular HTTP connection to the servers

play01:29

specifying a desired websocket endpoint

play01:31

in the URL usually prefixed with WS or

play01:35

WSS for a secure connections it sends an

play01:38

HTTP get request with a specific headers

play01:40

to indicate the intention to upgrade to

play01:43

websocket now the client set headers

play01:45

looks like this wherein the upgrade

play01:47

websocket headers tells the server that

play01:49

the client wants to upgrade the

play01:51

connection to websocket the connection

play01:53

upgrade header informs the server that

play01:55

the client desires an upgrade from the

play01:57

current connection protocol the SEC

play02:00

websocket key is the header that

play02:02

contains a randomly generated b64

play02:04

encoding string unique to this handshake

play02:07

it will be used by the server to verify

play02:08

the handshake later and then there are

play02:11

some additional headers depending on the

play02:12

server implementation and desired

play02:14

features the additional headers like SE

play02:17

websocket version specifying the

play02:19

websocket protocol version or SE

play02:21

websocket protocol requesting a specific

play02:23

sub protocol which might be included and

play02:25

then the client waits for the service

play02:27

response to the HTTP upgrade request on

play02:30

the other end when the server receives

play02:32

HTTP get request from the client it

play02:34

checks the request headers to ensure

play02:36

that they are valid for a websocket

play02:37

handshake and sends an HTTP response

play02:40

with a status code of 101 switching

play02:42

protocols to indicate successful upgrade

play02:45

to websocket the handshake from server

play02:47

looks like

play02:49

this let's go through these steps in

play02:52

detail the server first verifies the

play02:54

presence of upgrade websocket and

play02:57

connection upgrade headers now the

play02:59

primary goal of the server processing

play03:01

and generating the SE websocket accept

play03:03

header is to prove to the client that

play03:04

the server recognized it as a valid

play03:07

websocket hand check initiation and is

play03:09

willing to proceed and the server can

play03:11

confirm that the hand check request

play03:13

originated from a genuine web client and

play03:15

not a malicious actor attempting to

play03:17

impersonate a websocket connection the

play03:19

server receives the base 64 encoded

play03:20

string from the SE websocket key header

play03:23

the server then generates a new string

play03:25

by concatenating the received SE

play03:26

websocket key with a predefined u ID for

play03:29

example this and then applying a

play03:31

cryptographic hash function such as sha1

play03:34

and this new string is then Bas 64

play03:37

encoded the server sends an HTTP

play03:39

response with a status code of 101

play03:41

switching protocols to indicate

play03:43

successful upgrade to websocket the

play03:45

response includes the following

play03:47

headers an upgrade web socket header

play03:49

echoing back the upgrade request from

play03:51

the client connection upgrade confirming

play03:53

the upgrade from HTTP to websocket and

play03:56

SE websocket accept this is the base 64

play03:59

for encoded string generated previously

play04:02

and this verifies that the client

play04:03

initiated the hand check once the client

play04:06

receiv receives the service response

play04:08

with the correct SEC websocket accept

play04:09

header it validates the response

play04:12

confirming a successful hand check both

play04:14

client and server can now start

play04:16

exchanging data using the websocket

play04:17

protocol over the established TCP

play04:19

connection the HTTP connection is

play04:22

essentially replaced by the websocket

play04:24

connection now if the SE websocket

play04:27

accept value does not match the expected

play04:29

value or if the header field is missing

play04:31

or if the HTTP status code is not 101

play04:34

the connection will not be established

play04:36

and websocket Frames will not be sent if

play04:39

any code other than 101 is returned from

play04:41

the server clients have to end the

play04:43

connection the web socket handic ensures

play04:46

a secure and authenticated Connection by

play04:48

including a random key SEC websocket key

play04:51

generated by the client and verified by

play04:53

the server through SE websocket accept

play04:55

header this handshake process paves the

play04:58

way for realtime two we communication

play05:00

between the client and server without

play05:02

the need for a constant HTTP request and

play05:05

responses websocket has a default Ur

play05:07

format such as below like I said before

play05:10

it can be either WS or WSS just like

play05:13

HTTP protocol the port component is

play05:16

optional as default Port 0 is used for

play05:18

ws and 443 is used for

play05:21

WSS WSS is used as a secure URI as a

play05:26

secure flag is set and TLS handshake is

play05:28

done between server and L for secure

play05:32

Communication in websocket protocol data

play05:34

is transmitted using sequence of frames

play05:37

frames in web soet are the basic units

play05:39

of the data that are exchanged between

play05:40

the client and server and each frame has

play05:42

a specific structure fin or F indicates

play05:46

whether the frame is in the final

play05:47

fragment or larger message a value of

play05:50

one means it's the final fragment and

play05:52

while zero means there are more

play05:54

fragments to come more on fragments

play05:57

later RSV 1 rsv2 RSV three these three

play06:00

beds are the reserve bits they are

play06:02

typically set to zero and they are

play06:04

currently reserved for future use case

play06:06

or potential extensions to the websocket

play06:08

protocol the op code Fields defines the

play06:11

type of data the frame carries it could

play06:13

be a textual data in utf8 encoded format

play06:16

or a binary data or a ping frame for

play06:19

keep alive

play06:20

events The Mask pit indicates if the

play06:23

payo data is masked masking is always

play06:26

applied to frames sent from client to

play06:27

server we'll take a closer look at

play06:29

masking shortly the payload length

play06:32

defines the length of the payload data

play06:34

and the length field itself can vary

play06:36

from 0 to 125 depending on the size of

play06:38

the payload the masking key is used to

play06:41

obscure payload data and the payload

play06:44

data which is a variable length has the

play06:46

actual content of the messages such as

play06:48

text or binary data now coming back to

play06:51

masking masking main goal in websocket

play06:54

is to ensure smooth transition of

play06:56

real-time data through various Network

play06:57

infrastructure that wasn't necessarily

play06:59

designed with websockets in mind before

play07:02

websockets were standardized some

play07:04

Network infrastructure component

play07:06

particularly those designed purely for

play07:08

HTTP traffic try to be smart when it

play07:11

came to caching these caching proxies

play07:13

sometimes stored responses from a server

play07:15

expecting that they can later reuse the

play07:17

same response for similar HTTP request

play07:20

however websocket connections don't

play07:22

follow the traditional HTTP request

play07:24

response pattern unmasked websocket data

play07:26

could be wrongly interpreted as HTTP

play07:28

content and can can be stored

play07:30

potentially leading to issues later when

play07:32

a real HTTP request is made masking

play07:35

obscures the data within websocket

play07:37

frames in a way that makes it

play07:38

unpredictable and very unlikely to

play07:40

resemble valid HTTP request or responses

play07:43

this discourages proxies and other

play07:45

intermediaries from attempting to Cache

play07:47

or modify the data as it clearly doesn't

play07:50

fit the HTTP model masking basically

play07:53

makes web soer traffic look distinctly

play07:55

different from HTTP discouraging

play07:57

incorrect interpretation or manipulation

play07:59

fragmentation on the other hands in

play08:01

websockets refers to the process of

play08:02

splitting a large message into smaller

play08:04

chunks for transmission over the network

play08:07

this technique is particularly useful

play08:09

for handling a very large messages that

play08:11

might overwhelm the connection or exceed

play08:13

buffer limitations on either the client

play08:15

or server it is to prevent buffer

play08:17

overflow imagine a user uploading a

play08:19

large file through a websocket

play08:21

connection and without fragmentation the

play08:23

entire file would be sent as a single

play08:25

message this could potentially overflow

play08:27

buffers on the client or server leading

play08:29

to connection errors or performance

play08:31

issues fragmentation allows for gradual

play08:34

delivery of large messages the receiver

play08:36

can start processing the initial

play08:37

fragments while the remaining fragments

play08:39

are still being transmitted this can be

play08:42

beneficial for applications where

play08:44

immediate display of partial data is

play08:45

valuable such as receiving updates

play08:47

during a long running process now when a

play08:50

message is split up into fragments each

play08:52

fragment is sent with a fin bit set to

play08:54

zero for all but the final frame in the

play08:56

sequence the F bit indicates whether the

play08:59

current frame is the final frame in the

play09:01

message or whether more frames will

play09:03

follow if the fin bit is set to zero it

play09:05

means that there are more frames coming

play09:07

and the receiver should continue to wait

play09:09

for additional frames before processing

play09:10

the message and when the final fragment

play09:13

of the message is sent it is sent with

play09:15

the fin bit set to one this signals to

play09:17

the receiver that this is the last frame

play09:19

in the message now websockets are a

play09:21

versatile communication technology and

play09:23

find applications in various domains for

play09:25

example in chat applications for sending

play09:27

and receiving messages quickly and

play09:29

effici ly which I have in fact

play09:31

previously covered in my WhatsApp system

play09:32

design video it can also enable

play09:34

real-time Communication in web

play09:36

application such as changes and updates

play09:39

on the web pages in gaming industry it

play09:41

can facilitate seamless multiplayer

play09:43

gaming experiences or imagine a realtime

play09:45

drawing app where multiple users can

play09:47

collaborate on a canvas or maybe in

play09:49

stock exchanges for real-time updates on

play09:52

stock ticker prices these advanced

play09:53

websocket concepts might seem complex

play09:56

but the possibility is they unlock are

play09:57

worth the effort remember breaking

play09:59

things down experimenting and

play10:01

understanding the why behind each

play10:02

feature will lead to True Mastery so if

play10:05

you have any questions or cool project

play10:06

ideas drop them in the comments and

play10:08

let's keep the ban Community growing

play10:13

[Music]

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
WebSocketsReal-TimeCommunicationWebsockets HandshakeHTTP UpgradeProtocolsData FramesMaskingFragmentationWeb DevelopmentTech Tutorial