|
| 1 | +/* eslint-disable no-console */ |
| 2 | +const zmq = require('zeromq'); |
| 3 | + |
| 4 | +(async () => { |
| 5 | + const address = process.env.ADDR || 'tcp://127.0.0.1:7778'; |
| 6 | + const sock = new zmq.Router({ routingId: 'router-monitor-7778' }); |
| 7 | + |
| 8 | + // Monitor common router-side events |
| 9 | + const on = (name) => sock.events.on(name, (...args) => { |
| 10 | + const ts = new Date().toISOString(); |
| 11 | + console.log(`[${ts}] ROUTER events -> ${name}`, ...args); |
| 12 | + }); |
| 13 | + [ |
| 14 | + 'listening', |
| 15 | + 'accept', |
| 16 | + 'accept_error', |
| 17 | + 'bind_error', |
| 18 | + 'closed', |
| 19 | + 'close_error', |
| 20 | + 'disconnect', |
| 21 | + ].forEach(on); |
| 22 | + |
| 23 | + await sock.bind(address); |
| 24 | + console.log(`[router] bound to ${address}`); |
| 25 | + |
| 26 | + // Manual control: you can unbind/rebind the router yourself from the terminal |
| 27 | + // Example: |
| 28 | + // ADDR=tcp://127.0.0.1:7777 node scripts/zmq-monitor/router.js |
| 29 | + // Then in another terminal, stop and restart this process to simulate downtime |
| 30 | + |
| 31 | + // Track connected dealers |
| 32 | + const dealers = new Map(); // routingId -> { lastSeen, messageCount } |
| 33 | + |
| 34 | + console.log('[router] Waiting for messages...'); |
| 35 | + |
| 36 | + // Echo replies back to sender and track stats |
| 37 | + for await (const msg of sock) { |
| 38 | + console.log('[router] 🔔 Received message with', msg.length, 'frames'); |
| 39 | + |
| 40 | + // Router receives: [routingId, data] (2 frames) |
| 41 | + // ZeroMQ handles the empty delimiter internally in v6+ |
| 42 | + try { |
| 43 | + if (msg.length >= 2) { |
| 44 | + const [routingId, ...dataFrames] = msg; |
| 45 | + const routingIdStr = routingId.toString(); |
| 46 | + const data = dataFrames[0]?.toString() || ''; |
| 47 | + |
| 48 | + console.log(`[router] 🆔 Routing ID: ${routingIdStr}`); |
| 49 | + console.log(`[router] 📦 Data: ${data}`); |
| 50 | + |
| 51 | + // Track dealer stats |
| 52 | + if (!dealers.has(routingIdStr)) { |
| 53 | + dealers.set(routingIdStr, { lastSeen: Date.now(), messageCount: 0 }); |
| 54 | + console.log(`[router] 🔗 New dealer connected: ${routingIdStr}`); |
| 55 | + } |
| 56 | + |
| 57 | + const dealer = dealers.get(routingIdStr); |
| 58 | + dealer.lastSeen = Date.now(); |
| 59 | + dealer.messageCount++; |
| 60 | + |
| 61 | + // Log received message |
| 62 | + if (data.startsWith('ping-')) { |
| 63 | + console.log(`[router] 📩 Received from ${routingIdStr}: ${data} (total: ${dealer.messageCount})`); |
| 64 | + } else { |
| 65 | + console.log(`[router] 📩 Received from ${routingIdStr}: ${data}`); |
| 66 | + } |
| 67 | + |
| 68 | + // Echo back (router sends: [routingId, data]) |
| 69 | + await sock.send([routingId, ...dataFrames]); |
| 70 | + console.log(`[router] 📤 Echoed back to ${routingIdStr}`); |
| 71 | + } else { |
| 72 | + console.log(`[router] ⚠️ Unexpected frame count: ${msg.length}`); |
| 73 | + } |
| 74 | + } catch (e) { |
| 75 | + console.log('[router] ❌ Error:', e && e.message); |
| 76 | + console.log('[router] Stack:', e && e.stack); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + console.log('[router] Message loop ended'); |
| 81 | +})(); |
| 82 | + |
| 83 | + |
0 commit comments