Complete the code to start a long polling request from the client.
function startLongPolling() {
fetch('/events')
.then(response => response.[1]())
.then(data => {
console.log('Received:', data);
startLongPolling();
});
}The long polling response is usually plain text or JSON. Using text() reads the response as plain text, which is common for long polling.
Complete the server-side pseudo-code to send an SSE event.
function sendSSE(response, data) {
response.write('data: ' + [1] + '\n\n');
}Server-Sent Events send data as text lines. Using JSON.stringify(data) ensures complex data is sent as a JSON string.
Fix the error in the SSE client code to properly listen for messages.
const evtSource = new EventSource('/events'); evtSource.[1] = function(event) { console.log('Message:', event.data); };
The onmessage event handler listens for incoming SSE messages.
Fill both blanks to complete the long polling server logic that waits for new data before responding.
function handleLongPolling(request, response) {
waitForNewData(() => {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.[1](newData);
response.[2]();
});
}The server writes the new data with write() and then ends the response with end() to complete the request.
Fill all three blanks to complete the SSE server setup with correct headers and keep-alive logic.
function setupSSE(response) {
response.writeHead(200, {
'Content-Type': [1],
'Cache-Control': [2],
'Connection': [3]
});
}SSE requires Content-Type as text/event-stream, disables caching with no-cache, and keeps the connection alive with keep-alive.