-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (70 loc) · 2.3 KB
/
server.js
File metadata and controls
83 lines (70 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* server.js
*
* Main Entry Point for combined Soroban Indexer and WebSocket streaming server.
* This file orchestrates the real-time data flow from the blockchain to the frontend.
*/
const WebSocket = require('ws');
const wsEvents = require('./services/wsEvents');
const { startIndexer } = require('./services/eventIndexer');
// Config and Port Setup
const WS_PORT = process.env.WS_PORT || 3001;
const wss = new WebSocket.Server({ port: WS_PORT });
console.log('--- 🌐 TradeFlow Real-Time Stream Server ---');
console.log(`📡 WebSocket server running on ws://localhost:${WS_PORT}`);
// Connection tracking
let activeConnections = 0;
/**
* Listen for incoming WebSocket connections.
*/
wss.on('connection', (ws) => {
activeConnections++;
console.log(`✅ New Web3 client connected. Active: ${activeConnections}`);
// Initial Connection ACK
ws.send(JSON.stringify({
event: 'INDEXER_CONNECTED',
status: 'ONLINE',
timestamp: new Date().toISOString()
}));
ws.on('close', () => {
activeConnections--;
console.log(`❌ Web3 client disconnected. Active: ${activeConnections}`);
});
ws.on('error', (err) => {
console.error('⚠️ WS Socket Error:', err.message);
});
});
/**
* BROADCASTER: Listens to the internal 'newTrade' event emitter.
* Broadcasts every new blockchain event caught by the Indexer daemon
* to all connected browser clients.
*/
wsEvents.on('newTrade', (tradeData) => {
console.log(`📣 BROADCASTING: New trade found in pool ${tradeData.poolId.slice(0, 8)}...`);
const payload = JSON.stringify({
event: 'NEW_TRADE_EVENT',
data: tradeData,
receivedAt: new Date().toISOString()
});
// Iterative broadcast to all active subscribers
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(payload);
}
});
});
/**
* 🚀 START DAEMON ORCHESTRATION
* We start the Soroban Indexer in the same Node.js process to bridge
* the blockchain data with the WebSocket emitter via internal memory.
*/
startIndexer().catch((err) => {
console.error('❌ CRITICAL ERROR: Event Indexer failed to start:', err.message);
process.exit(1);
});
// Process Management
process.on('SIGTERM', () => {
console.log('🛑 Closing WebSocket connections and shutting down.');
wss.close();
process.exit(0);
});