0
0
HLDsystem_design~10 mins

Long polling and Server-Sent Events in HLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a long polling request from the client.

HLD
function startLongPolling() {
  fetch('/events')
    .then(response => response.[1]())
    .then(data => {
      console.log('Received:', data);
      startLongPolling();
    });
}
Drag options to blanks, or click blank then click option'
AarrayBuffer
Bjson
Cblob
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using json() when the server sends plain text.
Not calling the function recursively to continue polling.
2fill in blank
medium

Complete the server-side pseudo-code to send an SSE event.

HLD
function sendSSE(response, data) {
  response.write('data: ' + [1] + '\n\n');
}
Drag options to blanks, or click blank then click option'
Adata
BJSON.stringify(data)
Cdata.toString()
DString(data)
Attempts:
3 left
💡 Hint
Common Mistakes
Sending raw objects without stringifying.
Missing the double newline to end the event.
3fill in blank
hard

Fix the error in the SSE client code to properly listen for messages.

HLD
const evtSource = new EventSource('/events');
evtSource.[1] = function(event) {
  console.log('Message:', event.data);
};
Drag options to blanks, or click blank then click option'
Aonerror
Bonopen
Conmessage
Donconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'onopen' which fires when connection opens, not for messages.
Using 'onerror' which fires on errors, not messages.
4fill in blank
hard

Fill both blanks to complete the long polling server logic that waits for new data before responding.

HLD
function handleLongPolling(request, response) {
  waitForNewData(() => {
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.[1](newData);
    response.[2]();
  });
}
Drag options to blanks, or click blank then click option'
Aend
Bwrite
Csend
Dflush
Attempts:
3 left
💡 Hint
Common Mistakes
Calling end() before write(), causing no data sent.
Using send() which is not a standard Node.js response method.
5fill in blank
hard

Fill all three blanks to complete the SSE server setup with correct headers and keep-alive logic.

HLD
function setupSSE(response) {
  response.writeHead(200, {
    'Content-Type': [1],
    'Cache-Control': [2],
    'Connection': [3]
  });
}
Drag options to blanks, or click blank then click option'
A'text/event-stream'
B'no-cache'
C'keep-alive'
D'application/json'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'application/json' as Content-Type for SSE.
Omitting Cache-Control header causing caching issues.
Not setting Connection to keep-alive causing connection close.