Challenge - 5 Problems
WebSocket Mastery in Remix
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Understanding WebSocket connection lifecycle in Remix
Consider a Remix component that opens a WebSocket connection on mount and closes it on unmount. What will happen if the component is navigated away from before the connection closes?
Remix
import { useEffect } from 'react'; export default function Chat() { useEffect(() => { const socket = new WebSocket('wss://example.com/socket'); socket.onopen = () => console.log('Connected'); socket.onclose = () => console.log('Disconnected'); return () => { socket.close(); }; }, []); return <div>Chat Component</div>; }
Attempts:
2 left
💡 Hint
Think about what the cleanup function in useEffect does when the component unmounts.
✗ Incorrect
The cleanup function returned by useEffect runs when the component unmounts, so calling socket.close() properly closes the connection and triggers the onclose event.
❓ state_output
intermediate2:00remaining
WebSocket message state update in Remix component
Given the following Remix component code, what will be the value of the 'messages' state after receiving two messages 'Hello' and 'World' from the WebSocket?
Remix
import { useEffect, useState } from 'react'; export default function MessageList() { const [messages, setMessages] = useState([]); useEffect(() => { const socket = new WebSocket('wss://example.com/socket'); socket.onmessage = (event) => { setMessages(prev => [...prev, event.data]); }; return () => socket.close(); }, []); return <ul>{messages.map((msg, i) => <li key={i}>{msg}</li>)}</ul>; }
Attempts:
2 left
💡 Hint
Look at how setMessages updates the state using the previous state.
✗ Incorrect
Each message received appends to the previous messages array, so after two messages, the state contains both in order.
📝 Syntax
advanced2:00remaining
Correct WebSocket event handler syntax in Remix
Which option correctly sets up a WebSocket 'onmessage' event handler inside a Remix component's useEffect hook?
Remix
import { useEffect } from 'react'; export default function SocketComponent() { useEffect(() => { const socket = new WebSocket('wss://example.com/socket'); // Setup onmessage handler here return () => socket.close(); }, []); return <div>Socket Component</div>; }
Attempts:
2 left
💡 Hint
Remember the correct way to assign a function to an event handler property.
✗ Incorrect
The correct syntax assigns an arrow function to socket.onmessage. Other options have syntax errors.
🔧 Debug
advanced2:00remaining
Debugging WebSocket reconnection logic in Remix
A developer tries to implement automatic WebSocket reconnection in a Remix component but notices multiple connections open simultaneously. What is the most likely cause?
Remix
import { useEffect, useRef } from 'react'; export default function AutoReconnect() { const socketRef = useRef(null); useEffect(() => { function connect() { if (socketRef.current) { socketRef.current.close(); } socketRef.current = new WebSocket('wss://example.com/socket'); socketRef.current.onclose = () => { setTimeout(connect, 1000); }; } connect(); }, []); return <div>AutoReconnect Component</div>; }
Attempts:
2 left
💡 Hint
Check if previous WebSocket connections are closed before creating new ones.
✗ Incorrect
Each reconnection creates a new WebSocket without closing the old one, leading to multiple open connections.
🧠 Conceptual
expert3:00remaining
WebSocket integration with Remix server actions
In Remix, which approach best integrates WebSocket communication with server actions to update client UI in real-time?
Attempts:
2 left
💡 Hint
Consider the roles of server actions and WebSocket connections in Remix architecture.
✗ Incorrect
Server actions handle HTTP requests like form submissions, while WebSocket connections on the client enable real-time updates. Combining both properly leverages Remix features.