#!/usr/bin/env node /** * Demo script for Frontend Discovery Integration * Shows how the frontend displays primary node information */ const http = require('http'); const BASE_URL = 'http://localhost:3001'; function makeRequest(path, method = 'GET') { return new Promise((resolve, reject) => { const options = { hostname: 'localhost', port: 3001, path: path, method: method, headers: { 'Content-Type': 'application/json' } }; const req = http.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { const jsonData = JSON.parse(data); resolve({ status: res.statusCode, data: jsonData }); } catch (error) { resolve({ status: res.statusCode, data: data }); } }); }); req.on('error', (error) => { reject(error); }); req.end(); }); } async function showFrontendIntegration() { console.log('šŸš€ Frontend Discovery Integration Demo'); console.log('====================================='); console.log('This demo shows how the frontend displays primary node information.'); console.log('Open http://localhost:3001 in your browser to see the UI.'); console.log(''); try { // Check if backend is running const healthResponse = await makeRequest('/api/health'); console.log('āœ… Backend is running'); // Get discovery information const discoveryResponse = await makeRequest('/api/discovery/nodes'); console.log('\nšŸ“” Discovery Status:'); console.log(` Primary Node: ${discoveryResponse.data.primaryNode || 'None'}`); console.log(` Total Nodes: ${discoveryResponse.data.totalNodes}`); console.log(` Client Initialized: ${discoveryResponse.data.clientInitialized}`); if (discoveryResponse.data.nodes.length > 0) { console.log('\n🌐 Discovered Nodes:'); discoveryResponse.data.nodes.forEach((node, index) => { console.log(` ${index + 1}. ${node.ip}:${node.port} (${node.isPrimary ? 'PRIMARY' : 'secondary'})`); console.log(` Last Seen: ${node.lastSeen}`); }); } console.log('\nšŸŽÆ Frontend Display:'); console.log(' The frontend will show:'); if (discoveryResponse.data.primaryNode) { const status = discoveryResponse.data.clientInitialized ? 'āœ…' : 'āš ļø'; const nodeCount = discoveryResponse.data.totalNodes > 1 ? ` (${discoveryResponse.data.totalNodes} nodes)` : ''; console.log(` ${status} ${discoveryResponse.data.primaryNode}${nodeCount}`); } else if (discoveryResponse.data.totalNodes > 0) { const firstNode = discoveryResponse.data.nodes[0]; console.log(` āš ļø ${firstNode.ip} (No Primary)`); } else { console.log(' šŸ” No Nodes Found'); } console.log('\nšŸ’” To test the frontend:'); console.log(' 1. Open http://localhost:3001 in your browser'); console.log(' 2. Look at the cluster header for primary node info'); console.log(' 3. Send discovery messages: npm run test-discovery broadcast'); console.log(' 4. Watch the primary node display update in real-time'); } catch (error) { console.error('āŒ Error:', error.message); console.log('\nšŸ’” Make sure the backend is running: npm start'); } } // Run the demo showFrontendIntegration().catch(console.error);