What if your app could talk to the server instantly, like a live conversation?
Why WebSocket server and client in Deno? - Purpose & Use Cases
Imagine building a chat app where every message requires you to refresh the page to see new texts.
Or a live game scoreboard that updates only when you reload the browser.
Manually refreshing pages or polling the server repeatedly wastes time and bandwidth.
It feels slow, clunky, and users get frustrated waiting for updates.
WebSocket lets the server and client keep a constant open line to send messages instantly.
This means updates happen live without refreshing or delays.
setInterval(() => fetch('/messages').then(...), 5000);
const ws = new WebSocket('ws://server'); ws.onmessage = e => console.log(e.data);Real-time, two-way communication between browser and server for instant updates.
A live chat app where messages appear instantly as friends type, without refreshing.
Manual page refresh or polling is slow and inefficient.
WebSocket keeps a live connection open for instant data exchange.
This creates smooth, real-time apps like chats and live feeds.
Practice
Solution
Step 1: Understand WebSocket functionality
WebSocket allows a continuous open connection between client and server for real-time data exchange.Step 2: Compare with other options
Sending emails, serving static files, or running background tasks are unrelated to WebSocket's purpose.Final Answer:
To keep a live connection open for instant two-way communication -> Option AQuick Check:
WebSocket = live two-way connection [OK]
- Confusing WebSocket with HTTP request-response
- Thinking WebSocket sends emails
- Assuming WebSocket serves files
Solution
Step 1: Recall Deno WebSocket upgrade syntax
Deno usesDeno.upgradeWebSocket(request)to upgrade HTTP requests to WebSocket connections.Step 2: Check other options for correctness
Options B, C, and D use incorrect constructors or methods not available in Deno's standard library.Final Answer:
const { socket, response } = Deno.upgradeWebSocket(request); -> Option DQuick Check:
Deno.upgradeWebSocket() = correct upgrade method [OK]
- Using new WebSocket() on server side
- Calling non-existent WebSocketServer constructor
- Mixing client WebSocket API with server upgrade
const { socket, response } = Deno.upgradeWebSocket(request);
socket.onmessage = (e) => {
socket.send(`Received: ${e.data}`);
};Solution
Step 1: Analyze the onmessage handler
When a message arrives, the server sends back a string with 'Received: ' plus the message content.Step 2: Substitute the client message
Client sends 'hello', so server sends 'Received: hello'.Final Answer:
"Received: hello" -> Option CQuick Check:
Server echoes with prefix 'Received:' [OK]
- Thinking server echoes raw message without prefix
- Confusing e.data as string literal
- Assuming no response is sent
const { socket, response } = Deno.upgradeWebSocket(request);
socket.onmessage = (e) => {
socket.send(e.data.toUpperCase());
};
return response;Solution
Step 1: Check upgradeWebSocket usage
Deno.upgradeWebSocket is synchronous and returns socket and response correctly.Step 2: Verify response return and event handling
The response is returned properly, and onopen event is optional, not required.Final Answer:
No error, code is correct -> Option AQuick Check:
Code follows correct WebSocket server pattern [OK]
- Adding unnecessary await to upgradeWebSocket
- Forgetting to return response
- Thinking onopen event is mandatory
Solution
Step 1: Understand broadcasting concept
Broadcasting means sending a message to all connected clients, so all sockets must be stored.Step 2: Evaluate options for managing clients
Store all sockets in an array and loop to send messages to each socket stores sockets in an array and loops to send messages, which is correct. Options A, B, and D do not support broadcasting properly.Final Answer:
Store all sockets in an array and loop to send messages to each socket -> Option BQuick Check:
Broadcast = send to all sockets stored [OK]
- Overwriting socket variable losing previous clients
- Creating multiple servers instead of one
- Sending messages only to one client
Ready to prove it?
Pass the test and this pattern moves unknown
