DevPrep

All topics › Networking & APIs

What is WebSocket? URLSessionWebSocketTask

Middle PRO

Essence

WebSocket is a two-way communication protocol over a single TCP connection. The client starts with an ordinary HTTP request carrying Upgrade: websocket; after a 101 Switching Protocols response the connection becomes a full-duplex channel: both sides send messages (text or binary frames) at any time, with no new requests or headers. Schemes are ws:// and wss:// (over TLS; ATS requires wss).

Uses: chats, price tickers, live updates, collaborative editing, game lobbies — anything where the server must push data. For one-way notifications SSE (bytes(for:)) is often enough; for rare events, APNs.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store