@@ -48,10 +48,9 @@ const fetchTokenInfo = async () => {
48
48
49
49
fetchTokenInfo ();
50
50
```
51
-
52
51
## Real-Time Data Streaming (Premium plan or higher only)
53
52
54
- The library includes a ` Datastream ` class for real-time data updates:
53
+ The library includes a ` Datastream ` class for real-time data updates with an improved, intuitive API :
55
54
56
55
``` typescript
57
56
import { Datastream } from ' @solanatracker/data-api' ;
@@ -61,58 +60,106 @@ const dataStream = new Datastream({
61
60
wsUrl: ' YOUR_WS_URL'
62
61
});
63
62
64
- // Example: Subscribe to latest tokens
65
- dataStream .subscribeToLatest ();
63
+ // Connect to the WebSocket server
64
+ dataStream .connect ();
66
65
67
- // Listen for new tokens
68
- dataStream .on (' latest' , (tokenData ) => {
66
+ // Handle connection events
67
+ dataStream .on (' connected' , () => console .log (' Connected to datastream' ));
68
+ dataStream .on (' disconnected' , () => console .log (' Disconnected from datastream' ));
69
+ dataStream .on (' error' , (error ) => console .error (' Datastream error:' , error ));
70
+
71
+ // Example 1: Subscribe to latest tokens with chained listener
72
+ dataStream .subscribe .latest ().on ((tokenData ) => {
69
73
console .log (' New token created:' , tokenData .token .name );
70
74
});
71
75
72
- // Example: Track a specific token's price
76
+ // Example 2 : Track a specific token's price with type-safe data
73
77
const tokenAddress = ' 6p6xgHyF7AeE6TZkSmFsko444wqoP15icUSqi2jfGiPN' ; // TRUMP token
74
- dataStream .subscribeToTokenPrice (tokenAddress );
75
-
76
- // Listen for price updates
77
- dataStream .on (` price-by-token:${tokenAddress } ` , (priceData ) => {
78
+ dataStream .subscribe .price .token (tokenAddress ).on ((priceData ) => {
78
79
console .log (` New price: $${priceData .price } ` );
80
+ console .log (` Time: ${new Date (priceData .time ).toLocaleTimeString ()} ` );
79
81
});
80
82
81
- // Connect to the WebSocket server
82
- dataStream .connect ();
83
+ // Example 3: Subscribe to token transactions with stored subscription reference
84
+ const txSubscription = dataStream .subscribe .tx .token (tokenAddress ).on ((transaction ) => {
85
+ console .log (` Transaction type: ${transaction .type } ` );
86
+ console .log (` Amount: ${transaction .amount } ` );
87
+ console .log (` Price: $${transaction .priceUsd } ` );
88
+ });
83
89
84
- // Handle connection events
85
- dataStream .on (' connected' , () => console .log (' Connected to datastream' ));
86
- dataStream .on (' disconnected' , () => console .log (' Disconnected from datastream' ));
87
- dataStream .on (' error' , (error ) => console .error (' Datastream error:' , error ));
90
+ // Later, unsubscribe from transactions
91
+ txSubscription .unsubscribe ();
92
+
93
+ // Example 4: Monitor holder count for a token
94
+ dataStream .subscribe .holders (tokenAddress ).on ((holderData ) => {
95
+ console .log (` Total holders: ${holderData .total } ` );
96
+ });
97
+
98
+ // Example 5: Watch for wallet transactions
99
+ const walletAddress = ' YourWalletAddressHere' ;
100
+ dataStream .subscribe .tx .wallet (walletAddress ).on ((walletTx ) => {
101
+ console .log (` ${walletTx .type === ' buy' ? ' Bought' : ' Sold' } token ` );
102
+ console .log (` Volume: ${walletTx .volume } USD ` );
103
+ });
88
104
```
89
105
90
- Available subscription methods:
106
+ Available subscription methods are organized in a clean, intuitive namespace structure :
91
107
92
108
``` typescript
93
109
// Token and pool updates
94
- dataStream .subscribeToLatest ();
95
- dataStream .subscribeToTokenChanges (tokenAddress );
96
- dataStream .subscribeToPoolChanges (poolId );
110
+ dataStream .subscribe . latest (); // Latest tokens and pools
111
+ dataStream .subscribe . token (tokenAddress ); // Token changes (any pool)
112
+ dataStream .subscribe . pool (poolId ); // Pool changes
97
113
98
114
// Price updates
99
- dataStream .subscribeToTokenPrice (tokenAddress );
100
- dataStream .subscribeToPoolPrice (poolId );
115
+ dataStream .subscribe .price .token (tokenAddress ); // Token price (main pool)
116
+ dataStream .subscribe .price .allPoolsForToken (tokenAddress ); // All price updates for a token
117
+ dataStream .subscribe .price .pool (poolId ); // Pool price
101
118
102
119
// Transactions
103
- dataStream .subscribeToTokenTransactions (tokenAddress );
104
- dataStream .subscribeToPoolTransactions (tokenAddress , poolId );
105
- dataStream .subscribeToWalletTransactions (walletAddress );
120
+ dataStream .subscribe . tx . token (tokenAddress ); // Token transactions
121
+ dataStream .subscribe . tx . pool (tokenAddress , poolId ); // Pool transactions
122
+ dataStream .subscribe . tx . wallet (walletAddress ); // Wallet transactions
106
123
107
124
// Pump.fun stages
108
- dataStream .subscribeToGraduatingTokens ();
109
- dataStream .subscribeToGraduatedTokens ();
125
+ dataStream .subscribe . graduating (); // Graduating tokens
126
+ dataStream .subscribe . graduated (); // Graduated tokens
110
127
111
128
// Metadata and holders
112
- dataStream .subscribeToTokenMetadata (tokenAddress );
113
- dataStream .subscribeToHolderUpdates (tokenAddress );
129
+ dataStream .subscribe . metadata (tokenAddress ); // Token metadata
130
+ dataStream .subscribe . holders (tokenAddress ); // Holder updates
114
131
```
115
132
133
+ Each subscription method returns a response object with:
134
+ - ` room ` : The subscription channel name
135
+ - ` on() ` : Method to attach a listener with proper TypeScript types
136
+ - Returns an object with ` unsubscribe() ` method for easy cleanup
137
+
138
+ ## WebSocket Data Stream
139
+
140
+ The ` Datastream ` class provides real-time access to Solana Tracker data:
141
+
142
+ ### Events
143
+
144
+ The Datastream extends the standard EventEmitter interface, allowing you to listen for various events:
145
+
146
+ ``` typescript
147
+ // Connection events
148
+ dataStream .on (' connected' , () => console .log (' Connected to WebSocket server' ));
149
+ dataStream .on (' disconnected' , (socketType ) => console .log (` Disconnected: ${socketType } ` ));
150
+ dataStream .on (' reconnecting' , (attempt ) => console .log (` Reconnecting: attempt ${attempt } ` ));
151
+ dataStream .on (' error' , (error ) => console .error (' Error:' , error ));
152
+
153
+ // Data events - Standard approach
154
+ dataStream .on (' latest' , (data ) => console .log (' New token:' , data ));
155
+ dataStream .on (` price-by-token:${tokenAddress } ` , (data ) => console .log (' Price update:' , data ));
156
+ dataStream .on (` transaction:${tokenAddress } ` , (data ) => console .log (' New transaction:' , data ));
157
+
158
+ // New approach - Chain .on() directly to subscription
159
+ dataStream .subscribe .latest ().on ((data ) => console .log (' New token:' , data ));
160
+ dataStream .subscribe .price .token (tokenAddress ).on ((data ) => console .log (' Price update:' , data ));
161
+ dataStream .subscribe .tx .token (tokenAddress ).on ((data ) => console .log (' Transaction:' , data ));
162
+ ```
116
163
117
164
## API Documentation
118
165
@@ -319,6 +366,8 @@ dataStream.on('error', (error) => console.error('Error:', error));
319
366
// Data events
320
367
dataStream .on (' latest' , (data ) => console .log (' New token:' , data ));
321
368
dataStream .on (` price-by-token:${tokenAddress } ` , (data ) => console .log (' Price update:' , data ));
369
+ dataStream .on (` price:${tokenAddress } ` , (data ) => console .log (' Price update:' , data ));
370
+ dataStream .on (` price:${poolAddress } ` , (data ) => console .log (' Price update:' , data ));
322
371
dataStream .on (` transaction:${tokenAddress } ` , (data ) => console .log (' New transaction:' , data ));
323
372
dataStream .on (` wallet:${walletAddress } ` , (data ) => console .log (' Wallet transaction:' , data ));
324
373
dataStream .on (' graduating' , (data ) => console .log (' Graduating token:' , data ));
0 commit comments