How Web Sockets work | System Design Interview Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
websocket is a protocol that was developed for bi-directional web server browser Communication in real time that is a protocol is capable of sending and receiving data simultaneously our TCP connection typically in a standard HTTP web request the client opens the connection and requests data from the server the server then processes the request and builds the response and finally the server sends the response back to the client now you might have also heard about polling which I have described in one of my system design videos polling is a standard technique used by the vast majority of Ajax applications the basic idea is that the client repeatedly pulls or request a server for data the client makes a request and waits for the server to respond with data and if no data is available an empty response is written here is how the polling flow looks like the client opens a connection and requests data from the server using the regular HTTP protocol the server calculates the response and sends it back just like any regular HTTP traffic the client then sends requests to the server at regular intervals it could be every one second every 5 Seconds every 10 seconds or even in milliseconds now the problem with polling is that the client has to keep asking the server for any new data as a result there are a lot of new connections being created and lot of responses are empty creating a lot of HTTP overhead A variation of this traditional polling technique is HTTP long polling which allows the server to push information to the client whenever their data is available with long polling the client request information from the server exactly as in regular polling but with the expectation that the server may not respond immediately that's why this technique is sometimes referred to as hanging it that is if the server does not have any data available for the client instead of sending an empty response the server simply holds the request and waits until some data become available and once that data becomes available a complete response is sent to the client here is how the HTTP long polling flow looks like the client first makes an initial request using regular HTTP and waits for a response the server delays its response until it has an update and is available or a timeout has occurred when an update is available the server sends a complete response to the client the client typically sends a new longboard request either immediately upon receiving a response or after a pause to allow an acceptable latency period each long fall request has a timeout therefore the client has to reconnect periodically after the connection is closed due to timeouts clearly with HTTP request the client needs to send a request to the server and once a response is received from the server the connection is closed which means every client request creates a new connection and needs to wait for the full transaction between the server and client now HTTP long polling does allow the server to send the data independently or push data to the client without the client making a request however each longboard request has a timeout therefore the client has to reconnect periodically after the connection is closed due to timeouts and so long polling can be more resource intensive web sockets on the other hand can keep a unique Connection open while eliminating the latency problems that arise with long pulling websocket is basically a full duplex asynchronous messaging that is supported so that both the client and the server can stream messages to each other independently websocket protocol is capable of sending and receiving data simultaneously over TCP connection though websocket protocol was crafted for implementation across browser and servers it can be also used in messaging apps such as WhatsApp or Facebook Messenger websocket typically Works in three stages a client establishes an appropriate web sockets connection using a websocket handshake if the respective server supports the protocol it responds via the header to complete the hand check and then a websocket connection replaces the handshake with same TCP connection at this point both parties can start sending data typically when a websocket protocol is used a websocket Handler will be connected by the user websocket Handler is a lightweight server machine which keeps an open connection with all the active users so what are the application of websockets well web sockets are heavily used in real-time web application to show the data at the client end which is continuously being sent by the backend server for example in a stock trading website for displaying the stock prices fluctuation and data which is continuously pushed by the backend server to the client chat application use websockets to establish a long connection only once for exchange publishing and broadcasting of messages among the individual users or group of users chat application reuse the same websocket connection for sending and receiving the message gaming application or another place where web sockets are heavily used because they too require the UI gets automatically refreshed without even establishing a new connection however websockets will be an Overkill if you want to fetch old data or we want to get the data only once to process it data which is not required very frequently or Fest only once can be queried by the simple HTTP request so in this scenario it is better not to use websocket but go with HTTP protocol [Music]
Info
Channel: ByteMonk
Views: 171,052
Rating: undefined out of 5
Keywords: web sockets, web socket, socket, interview, faang, maang, http, long polling, polling, http long polling
Id: pnj3Jbho5Ck
Channel Id: undefined
Length: 5min 28sec (328 seconds)
Published: Thu Nov 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.