📖 Scenario: You want to create a simple chat system where a server can send messages to connected clients in real-time using WebSockets. This is like a walkie-talkie where messages are instantly shared.
🎯 Goal: Build a WebSocket server that accepts client connections and a client that connects to the server and sends a message. The server should broadcast received messages to all connected clients.
📋 What You'll Learn
Create a WebSocket server that listens on port 8080
Create a client that connects to the WebSocket server
Send a message from the client to the server
Broadcast messages from the server to all connected clients
💡 Why This Matters
🌍 Real World
WebSockets are used in chat apps, live notifications, multiplayer games, and real-time dashboards where instant communication is needed.
💼 Career
Understanding WebSocket servers and clients is important for backend and frontend developers working on real-time web applications.
Progress0 / 4 steps
1
Create a WebSocket server listening on port 8080
Write code to create a WebSocket server using Deno's standard library that listens on port 8080. Store the server listener in a variable called server.
Deno
Hint
Use serve from Deno's standard HTTP server module and specify port: 8080.
2
Accept WebSocket connections and store clients
Add code to accept incoming HTTP requests from server. Upgrade requests to WebSocket connections and store each connected WebSocket in a clients set.
Deno
Hint
Use Deno.upgradeWebSocket(req) to upgrade HTTP requests to WebSocket connections. Store sockets in a Set called clients.
3
Broadcast messages received from clients to all clients
Inside the WebSocket connection handler, add an onmessage event that sends the received message data to all connected clients in the clients set.
Deno
Hint
Use socket.onmessage to listen for messages and loop over clients to send the message to each.
4
Create a WebSocket client that connects and sends a message
Write code to create a WebSocket client that connects to ws://localhost:8080. After connection, send the message "Hello from client".
Deno
Hint
Create a WebSocket object with the server URL and send a message inside onopen.
Practice
(1/5)
1. What is the main purpose of using WebSocket in a Deno application?
easy
A. To keep a live connection open for instant two-way communication
B. To send emails from the server
C. To serve static HTML files
D. To run background tasks without user interaction
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 A
Quick Check:
WebSocket = live two-way connection [OK]
Hint: WebSocket means live chat-like connection [OK]
Common Mistakes:
Confusing WebSocket with HTTP request-response
Thinking WebSocket sends emails
Assuming WebSocket serves files
2. Which of the following is the correct way to upgrade an HTTP request to a WebSocket connection in Deno?
easy
A. const { socket, response } = WebSocket.upgrade(request);
B. const socket = new WebSocket(request);
C. const ws = new WebSocketServer(request);
D. const { socket, response } = Deno.upgradeWebSocket(request);
Solution
Step 1: Recall Deno WebSocket upgrade syntax
Deno uses Deno.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 D
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 A
Quick Check:
Code follows correct WebSocket server pattern [OK]
Hint: upgradeWebSocket is synchronous; return response is required [OK]
Common Mistakes:
Adding unnecessary await to upgradeWebSocket
Forgetting to return response
Thinking onopen event is mandatory
5. You want to build a Deno WebSocket server that broadcasts any received message to all connected clients. Which approach correctly manages multiple clients?
hard
A. Use a single socket variable and overwrite it on each connection
B. Store all sockets in an array and loop to send messages to each socket
C. Create a new WebSocket server for each client connection
D. Send messages only to the last connected client
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 B
Quick Check:
Broadcast = send to all sockets stored [OK]
Hint: Keep all sockets in a list to broadcast messages [OK]