Skip to content

Commit 451b98a

Browse files
authored
Merge pull request #56 from helius-labs/ping-metadata
Add 30s pinging to all 3 clients
2 parents d0c8957 + dfbbc59 commit 451b98a

20 files changed

+141
-208
lines changed

go/examples/account-sub.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ func main() {
1616

1717
godotenv.Load("../.env")
1818

19-
endpoint := os.Getenv("LASERSTREAM_PRODUCTION_ENDPOINT")
20-
if endpoint == "" {
21-
log.Fatal("LASERSTREAM_PRODUCTION_ENDPOINT required")
22-
}
23-
apiKey := os.Getenv("LASERSTREAM_PRODUCTION_API_KEY")
24-
if apiKey == "" {
25-
log.Fatal("LASERSTREAM_PRODUCTION_API_KEY required")
26-
}
19+
endpoint := "your-endpoint"
20+
apiKey := "your-api-key"
2721

2822
clientConfig := laserstream.LaserstreamConfig{
2923
Endpoint: endpoint,
@@ -43,7 +37,7 @@ func main() {
4337
client := laserstream.NewClient(clientConfig)
4438

4539
dataCallback := func(data *laserstream.SubscribeUpdate) {
46-
log.Printf("Account Update: %+v", data)
40+
log.Print(data)
4741
}
4842

4943
errorCallback := func(err error) {

go/examples/transaction-sub.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ func main() {
1616

1717
godotenv.Load("../.env")
1818

19-
endpoint := os.Getenv("LASERSTREAM_PRODUCTION_ENDPOINT")
20-
if endpoint == "" {
21-
log.Fatal("LASERSTREAM_PRODUCTION_ENDPOINT required")
22-
}
23-
apiKey := os.Getenv("LASERSTREAM_PRODUCTION_API_KEY")
24-
if apiKey == "" {
25-
log.Fatal("LASERSTREAM_PRODUCTION_API_KEY required")
26-
}
19+
endpoint := "your-endpoint"
20+
apiKey := "your-api-key"
2721

2822
clientConfig := laserstream.LaserstreamConfig{
2923
Endpoint: endpoint,
@@ -45,7 +39,7 @@ func main() {
4539
client := laserstream.NewClient(clientConfig)
4640

4741
dataCallback := func(data *laserstream.SubscribeUpdate) {
48-
log.Printf("Transaction Update: %+v", data)
42+
log.Print(data)
4943
}
5044

5145
errorCallback := func(err error) {

go/laserstream.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,32 @@ func (c *Client) handleStream(ctx context.Context, stream pb.Geyser_SubscribeCli
364364
}
365365
}()
366366

367+
// Start periodic ping goroutine
368+
pingCtx, cancelPing := context.WithCancel(ctx)
369+
defer cancelPing()
370+
371+
go func() {
372+
ticker := time.NewTicker(30 * time.Second)
373+
defer ticker.Stop()
374+
375+
for {
376+
select {
377+
case <-pingCtx.Done():
378+
return
379+
case <-ticker.C:
380+
pingReq := &SubscribeRequest{
381+
Ping: &SubscribeRequestPing{
382+
Id: int32(time.Now().UnixMilli()),
383+
},
384+
}
385+
if err := stream.Send(pingReq); err != nil {
386+
// If ping fails, let main stream handler deal with reconnection
387+
return
388+
}
389+
}
390+
}
391+
}()
392+
367393
for {
368394
select {
369395
case <-ctx.Done():

javascript/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "laserstream-napi"
3-
version = "0.1.7"
3+
version = "0.2.0"
44
edition = "2021"
55

66
[lib]

javascript/examples/account-sub.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import * as bs58 from 'bs58';
1010
const credentials = require('../test-config');
1111

1212
async function main() {
13-
console.log('🏦 Laserstream Account Subscription Example');
1413

1514
const config: LaserstreamConfig = {
16-
apiKey: credentials.laserstreamProduction.apiKey,
17-
endpoint: credentials.laserstreamProduction.endpoint,
15+
apiKey: "your-api-key",
16+
endpoint: "your-endpoint",
1817
};
1918

2019
const request = {
@@ -39,30 +38,17 @@ async function main() {
3938
config,
4039
request,
4140
async (update: SubscribeUpdate) => {
42-
if (update.account) {
43-
const accountUpdate: SubscribeUpdateAccount = update.account;
44-
console.log('\n🏦 Account Update Received!');
45-
console.log(' - Slot:', accountUpdate.slot);
46-
console.log(' - Is Startup:', accountUpdate.isStartup);
47-
48-
if (accountUpdate.account) {
49-
const accountInfo: SubscribeUpdateAccountInfo = accountUpdate.account;
50-
console.log(' - Account Info:');
51-
console.log(' - Pubkey:', accountInfo.pubkey ? bs58.encode(accountInfo.pubkey) : 'N/A');
52-
console.log(' - Lamports:', accountInfo.lamports);
53-
console.log(' - Owner:', accountInfo.owner ? bs58.encode(accountInfo.owner) : 'N/A');
54-
console.log(' - Executable:', accountInfo.executable);
55-
console.log(' - Rent Epoch:', accountInfo.rentEpoch);
56-
console.log(' - Data Length:', accountInfo.data ? accountInfo.data.length : 0);
57-
console.log(' - Write Version:', accountInfo.writeVersion);
58-
console.log(' - Txn Signature:', accountInfo.txnSignature ? bs58.encode(accountInfo.txnSignature) : 'N/A');
59-
}
60-
}
41+
console.log(JSON.stringify(update, null, 2));
6142
},
62-
async (err) => console.error('❌ Stream error:', err)
43+
(error: Error) => {
44+
console.error("Stream error:", error);
45+
}
6346
);
6447

65-
console.log(`✅ Account subscription started (id: ${stream.id})`);
48+
process.on("SIGINT", () => {
49+
stream.cancel();
50+
process.exit(0);
51+
});
6652
}
6753

6854
main().catch(console.error);

javascript/examples/accounts-data-slice-sub.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { subscribe, CommitmentLevel, SubscribeUpdate, LaserstreamConfig } from '
22
const credentials = require('../test-config');
33

44
async function main() {
5-
console.log('🔍 LaserStream Accounts Data Slice Subscription Example');
65

76
const config: LaserstreamConfig = {
8-
apiKey: credentials.laserstreamProduction.apiKey,
9-
endpoint: credentials.laserstreamProduction.endpoint,
7+
apiKey: "your-api-key",
8+
endpoint: "your-endpoint",
109
};
1110

1211
const request = {
@@ -36,12 +35,17 @@ async function main() {
3635
config,
3736
request,
3837
async (update: SubscribeUpdate) => {
39-
console.log(update);
38+
console.log(JSON.stringify(update, null, 2));
4039
},
41-
async (err) => console.error('❌ Stream error:', err)
40+
(error: Error) => {
41+
console.error("Stream error:", error);
42+
}
4243
);
4344

44-
console.log(`✅ Accounts data slice subscription started (id: ${stream.id})`);
45+
process.on("SIGINT", () => {
46+
stream.cancel();
47+
process.exit(0);
48+
});
4549
}
4650

47-
main().catch(console.error);
51+
main().catch(console.error);

javascript/examples/block-meta-sub.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import {
88
const credentials = require('../test-config');
99

1010
async function main() {
11-
console.log('🏗️ Laserstream Block Meta Subscription Example');
1211

1312
const config: LaserstreamConfig = {
14-
apiKey: credentials.laserstreamProduction.apiKey,
15-
endpoint: credentials.laserstreamProduction.endpoint,
13+
apiKey: "your-api-key",
14+
endpoint: "your-endpoint",
1615
};
1716

1817
const request = {
@@ -38,32 +37,17 @@ async function main() {
3837
config,
3938
request,
4039
async (update: SubscribeUpdate) => {
41-
if (update.blockMeta) {
42-
const blockMeta: SubscribeUpdateBlockMeta = update.blockMeta;
43-
console.log('\n🏗️ Block Meta Update Received!');
44-
console.log(' - Slot:', blockMeta.slot);
45-
console.log(' - Blockhash:', blockMeta.blockhash);
46-
console.log(' - Parent Slot:', blockMeta.parentSlot);
47-
console.log(' - Parent Blockhash:', blockMeta.parentBlockhash);
48-
console.log(' - Block Height:', blockMeta.blockHeight?.blockHeight || 'N/A');
49-
console.log(' - Block Time:', blockMeta.blockTime?.timestamp || 'N/A');
50-
console.log(' - Executed Transaction Count:', blockMeta.executedTransactionCount);
51-
console.log(' - Rewards:', blockMeta.rewards?.rewards?.length || 0);
52-
}
40+
console.log(JSON.stringify(update, null, 2));
5341
},
54-
async (error: any) => {
55-
console.error('❌ Stream error:', error);
42+
(error: Error) => {
43+
console.error("Stream error:", error);
5644
}
5745
);
5846

59-
console.log(`✅ Block Meta subscription started with ID: ${stream.id}`);
60-
61-
// Cleanup on exit
62-
process.on('SIGINT', () => {
63-
console.log('\n🛑 Cancelling stream...');
47+
process.on("SIGINT", () => {
6448
stream.cancel();
6549
process.exit(0);
6650
});
6751
}
6852

69-
main().catch(console.error);
53+
main().catch(console.error);

javascript/examples/block-sub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ async function main() {
4747
await new Promise(() => {});
4848
}
4949

50-
main().catch(console.error);
50+
main().catch(console.error);

javascript/examples/entry-sub.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import * as bs58 from 'bs58';
99
const credentials = require('../test-config');
1010

1111
async function runEntrySubscription() {
12-
console.log('📝 Laserstream Entry Subscription Example');
1312

1413
const config: LaserstreamConfig = {
15-
apiKey: credentials.laserstreamProduction.apiKey,
16-
endpoint: credentials.laserstreamProduction.endpoint,
14+
apiKey: "your-api-key",
15+
endpoint: "your-endpoint",
1716
};
1817

1918
// Subscribe to entry updates
@@ -35,30 +34,17 @@ async function runEntrySubscription() {
3534
config,
3635
request,
3736
async (update: SubscribeUpdate) => {
38-
if (update.entry) {
39-
const entryUpdate: SubscribeUpdateEntry = update.entry;
40-
console.log('\n📝 Entry Update Received!');
41-
console.log(' - Slot:', entryUpdate.slot);
42-
console.log(' - Index:', entryUpdate.index);
43-
console.log(' - Num Hashes:', entryUpdate.numHashes);
44-
console.log(' - Hash:', entryUpdate.hash ? bs58.encode(entryUpdate.hash) : 'N/A');
45-
console.log(' - Executed Transaction Count:', entryUpdate.executedTransactionCount);
46-
console.log(' - Starting Transaction Index:', entryUpdate.startingTransactionIndex);
47-
}
37+
console.log(JSON.stringify(update, null, 2));
4838
},
49-
async (error: Error) => {
50-
console.error('Stream error:', error);
39+
(error) => {
40+
console.error('Stream error:', error);
5141
}
5242
);
5343

54-
console.log(`✅ Entry subscription started with ID: ${stream.id}`);
55-
56-
// Cleanup on exit
5744
process.on('SIGINT', () => {
58-
console.log('\n🛑 Cancelling stream...');
5945
stream.cancel();
6046
process.exit(0);
6147
});
6248
}
6349

64-
runEntrySubscription().catch(console.error);
50+
runEntrySubscription().catch(console.error);

0 commit comments

Comments
 (0)