@@ -48,10 +48,9 @@ const fetchTokenInfo = async () => {
4848
4949fetchTokenInfo ();
5050```
51-
5251## Real-Time Data Streaming (Premium plan or higher only)
5352
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 :
5554
5655``` typescript
5756import { Datastream } from ' @solanatracker/data-api' ;
@@ -61,58 +60,106 @@ const dataStream = new Datastream({
6160 wsUrl: ' YOUR_WS_URL'
6261});
6362
64- // Example: Subscribe to latest tokens
65- dataStream .subscribeToLatest ();
63+ // Connect to the WebSocket server
64+ dataStream .connect ();
6665
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 ) => {
6973 console .log (' New token created:' , tokenData .token .name );
7074});
7175
72- // Example: Track a specific token's price
76+ // Example 2 : Track a specific token's price with type-safe data
7377const 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 ) => {
7879 console .log (` New price: $${priceData .price } ` );
80+ console .log (` Time: ${new Date (priceData .time ).toLocaleTimeString ()} ` );
7981});
8082
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+ });
8389
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+ });
88104```
89105
90- Available subscription methods:
106+ Available subscription methods are organized in a clean, intuitive namespace structure :
91107
92108``` typescript
93109// 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
97113
98114// 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
101118
102119// 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
106123
107124// Pump.fun stages
108- dataStream .subscribeToGraduatingTokens ();
109- dataStream .subscribeToGraduatedTokens ();
125+ dataStream .subscribe . graduating (); // Graduating tokens
126+ dataStream .subscribe . graduated (); // Graduated tokens
110127
111128// 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
114131```
115132
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+ ```
116163
117164## API Documentation
118165
@@ -319,6 +366,8 @@ dataStream.on('error', (error) => console.error('Error:', error));
319366// Data events
320367dataStream .on (' latest' , (data ) => console .log (' New token:' , data ));
321368dataStream .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 ));
322371dataStream .on (` transaction:${tokenAddress } ` , (data ) => console .log (' New transaction:' , data ));
323372dataStream .on (` wallet:${walletAddress } ` , (data ) => console .log (' Wallet transaction:' , data ));
324373dataStream .on (' graduating' , (data ) => console .log (' Graduating token:' , data ));
0 commit comments