API Fundamentals Course
API Fundamentals
/
Advanced

WebSockets

Definition

A computer communications protocol providing full-duplex, continuous, two-way communication channels over a single TCP connection.

Explain Like I'm New

Standard HTTP is like sending a letter. You ask, they answer, the connection closes. WebSockets is like a phone call. You call the server, the line stays open permanently. You can talk at any time, and the server can talk back at any time, instantly.

Real World Example

Multiplayer browser games, live chat applications (Discord), and live stock trading charts. The server can push data to the client without the client ever asking for it.

Common Use Cases

  • •Live chat
  • •Real-time gaming
  • •Collaborative editing (Google Docs)

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • In a standard HTTP request, who MUST initiate the conversation: The Client or the Server?

intermediate

  • What is the difference between WebSockets and standard HTTP Polling?

Flash Cards

Question

Who must initiate?

Click to reveal answer
Answer

The Client. In standard HTTP, a Server can NEVER send data to a client unless the client explicitly asked for it first.

Question

WebSockets vs Polling?

Click to reveal answer
Answer

Polling is the client asking the server 'Any new messages?' every 2 seconds. It wastes massive amounts of bandwidth. WebSockets keep a silent connection open, and the server instantly pushes the message only when a new message actually arrives.