Challenge - 5 Problems
WebSocket Mastery in Deno
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWebSocket Server Message Broadcast Behavior
Consider a Deno WebSocket server that accepts multiple clients. When one client sends a message, the server broadcasts it to all connected clients including the sender.
What will be the output on the clients if client A sends the message 'hello'?
What will be the output on the clients if client A sends the message 'hello'?
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; const clients = new Set(); serve(async (req) => { if (req.headers.get("upgrade") !== "websocket") { return new Response("Not a websocket request", { status: 400 }); } const { socket, response } = Deno.upgradeWebSocket(req); clients.add(socket); socket.onmessage = (e) => { for (const client of clients) { client.send(e.data); } }; socket.onclose = () => { clients.delete(socket); }; return response; });
Attempts:
2 left
💡 Hint
The server sends the received message to every connected client in the set.
✗ Incorrect
The server loops through all connected clients and sends the message to each, including the sender. So client A also receives its own message.
📝 Syntax
intermediateCorrect WebSocket Client Connection Syntax in Deno
Which of the following code snippets correctly creates a WebSocket client connection to ws://localhost:8080 in Deno?
Attempts:
2 left
💡 Hint
WebSocket URLs start with ws:// for unencrypted connections.
✗ Incorrect
The WebSocket protocol uses ws:// for normal connections and wss:// for secure connections. Using http:// or ftp:// is invalid for WebSocket.
❓ state_output
advancedWebSocket Client Message State After Receiving Multiple Messages
A Deno WebSocket client connects to a server and listens for messages. It stores all received messages in an array called 'messages'.
Given the code below, what is the value of 'messages' after receiving three messages: 'one', 'two', 'three'?
Given the code below, what is the value of 'messages' after receiving three messages: 'one', 'two', 'three'?
Deno
const messages = [];
const ws = new WebSocket('ws://localhost:8080');
ws.onmessage = (event) => {
messages.push(event.data);
};Attempts:
2 left
💡 Hint
Each message triggers the onmessage event and appends to the array.
✗ Incorrect
The onmessage handler pushes every received message data into the 'messages' array, so all three messages are stored in order.
🔧 Debug
advancedIdentify the Error in WebSocket Server Close Handling
A Deno WebSocket server tries to remove a client socket from the clients set when it closes. However, the client never gets removed.
What is the error in the following code snippet?
What is the error in the following code snippet?
Deno
const clients = new Set();
socket.onclose = () => {
clients.delete();
};Attempts:
2 left
💡 Hint
The delete method requires the item to remove as an argument.
✗ Incorrect
The delete method on a Set requires the specific item to remove. Calling clients.delete() without arguments does nothing.
🧠 Conceptual
expertWebSocket Server Handling Multiple Clients Concurrently
In a Deno WebSocket server, what is the best way to handle multiple clients sending messages at the same time to avoid race conditions when broadcasting messages?
Attempts:
2 left
💡 Hint
Think about how to keep message order consistent and avoid conflicts.
✗ Incorrect
Using a queue ensures messages are processed in order and broadcasts happen one at a time, preventing race conditions.
