-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression-zstd-example.ts
More file actions
69 lines (59 loc) · 2.08 KB
/
compression-zstd-example.ts
File metadata and controls
69 lines (59 loc) · 2.08 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
import { subscribe, CommitmentLevel, SubscribeUpdate, LaserstreamConfig, CompressionAlgorithms } from '../client';
async function main() {
// Try to load from test config or env
let apiKey, endpoint;
try {
const credentials = require('../test-config');
apiKey = credentials.laserstreamProduction.apiKey;
endpoint = credentials.laserstreamProduction.endpoint;
} catch (e) {
apiKey = process.env.LASERSTREAM_PRODUCTION_API_KEY || process.env.HELIUS_API_KEY;
endpoint = process.env.LASERSTREAM_PRODUCTION_ENDPOINT || process.env.LASERSTREAM_ENDPOINT;
}
if (!apiKey || !endpoint) {
console.error('Please set LASERSTREAM_PRODUCTION_API_KEY or HELIUS_API_KEY and endpoint');
process.exit(1);
}
const config: LaserstreamConfig = {
apiKey,
endpoint,
maxReconnectAttempts: 10,
channelOptions: {
'grpc.default_compression_algorithm': CompressionAlgorithms.zstd, // Use zstd compression
'grpc.max_receive_message_length': 1_000_000_000, // 1GB
'grpc.max_send_message_length': 32_000_000, // 32MB
'grpc.keepalive_time_ms': 30000,
'grpc.keepalive_timeout_ms': 5000,
}
};
const request = {
slots: {
"compressed-slots": {}
},
commitment: CommitmentLevel.PROCESSED,
};
console.log('🚀 Starting stream with zstd compression (more efficient than gzip!)...');
let slotCount = 0;
const maxSlots = 10;
const stream = await subscribe(
config,
request,
async (update: SubscribeUpdate) => {
if (update.slot) {
slotCount++;
console.log(`✅ Received zstd compressed slot update #${slotCount}: slot=${update.slot.slot}`);
if (slotCount >= maxSlots) {
console.log(`\n🎉 Received ${maxSlots} zstd compressed slot updates. Stopping...`);
stream.cancel();
process.exit(0);
}
}
},
async (err) => {
console.error('❌ Stream error:', err);
}
);
console.log(`✅ Stream connected with ID: ${stream.id}`);
console.log('🔄 Using zstd compression for maximum efficiency');
}
main().catch(console.error);