跳转到主要内容
不漏掉任何一步:LaserStream的历史重播确保您可以从断开连接中恢复,并补充过去24小时的区块链活动中遗漏的数据。

什么是历史重播?

历史重放是LaserStream的一项功能,可以重放过去最多216,000个插槽的最近区块链数据(大约24小时的区块链活动)。这对于处理断线并确保实时应用中的数据连续性很有用。
时间窗口有限:历史重播目前限制在过去24小时的区块链活动中。您无法重播过去任意时间点的数据。

处理断开连接

恢复短暂断开连接时丢失的数据(最多24小时)

启动应用程序

使用最近24小时的上下文启动应用程序

分析最近事件

查看最近的交易和账户变化

使用最近数据测试

使用真实的最近数据进行测试和开发

工作原理

1

指定起始点

使用 fromSlot 参数设置重放的起始点(必须在最近~216,000个插槽内)
2

流式传输历史数据

LaserStream 从您指定的插槽开始传送所有事件
3

赶超至实时

历史数据流传输直到您达到当前插槽
4

继续实时流传输

无缝过渡到实时数据流传输
自动重新连接LaserStream SDK 自动处理重新连接和重播。无需额外代码!

快速入门

有兴趣尝试 LaserStream 吗?申请免费试用
import { subscribe, CommitmentLevel, LaserstreamConfig, SubscribeRequest } from 'helius-laserstream';

const subscriptionRequest: SubscribeRequest = {
  transactions: {
    client: {
      accountInclude: ['TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'],
      vote: false,
      failed: false
    }
  },
  commitment: CommitmentLevel.CONFIRMED,
  accounts: {},
  slots: {},
  blocks: {},
  blocksMeta: {},
  entry: {},
  accountsDataSlice: [],
  fromSlot: '224339000' // Start replay from this slot (must be within last ~216,000 slots)
};

const config: LaserstreamConfig = {
  apiKey: 'YOUR_API_KEY',
  endpoint: 'https://laserstream-mainnet-ewr.helius-rpc.com', // Choose your closest region
};

await subscribe(config, subscriptionRequest, 
  async (data) => {
    console.log('Received data:', data);
  }, 
  async (error) => {
    console.error('Error:', error);
  }
);

配置选项

fromSlot
string
必填
开始重放的插槽号。必须在重放窗口内(当前插槽的最近~216,000个插槽内)。示例: "224339000"重要: 插槽必须足够新,才能在24小时的重放窗口内。

使用案例

当应用在短暂断线(低于24小时)后重新连接时,可以使用历史重放以确保没有数据丢失:
// Store the last processed slot
let lastProcessedSlot = getLastProcessedSlot();

// Check if the slot is still within the replay window
const currentSlot = await getCurrentSlot();
const maxReplaySlot = currentSlot - 216000;

if (lastProcessedSlot < maxReplaySlot) {
  console.warn('Disconnection too long, some data may be lost');
  lastProcessedSlot = maxReplaySlot;
}

const subscriptionRequest: SubscribeRequest = {
  // ... your subscription config
  fromSlot: lastProcessedSlot.toString()
};

await subscribe(config, subscriptionRequest, 
  async (data) => {
    // Process data and update last processed slot
    await processData(data);
    lastProcessedSlot = data.slot;
    saveLastProcessedSlot(lastProcessedSlot);
  }
);
从最后几分钟的最近上下文开始应用:
// Get a slot from 10 minutes ago (within the 24-hour window)
const currentSlot = await getCurrentSlot();
const startSlot = currentSlot - 1500; // ~10 minutes ago

const subscriptionRequest: SubscribeRequest = {
  // ... your subscription config
  fromSlot: startSlot.toString()
};
使用最近的历史数据进行测试(限最近24小时内):
// Test with data from the last 5 minutes
const currentSlot = await getCurrentSlot();
const testStartSlot = currentSlot - 750; // ~5 minutes ago
const testEndSlot = currentSlot - 150; // ~1 minute ago

const subscriptionRequest: SubscribeRequest = {
  // ... your subscription config
  fromSlot: testStartSlot.toString()
};

// Stop processing when reaching test end slot
await subscribe(config, subscriptionRequest, 
  async (data) => {
    if (data.slot >= testEndSlot) {
      // Stop processing
      return;
    }
    await processTestData(data);
  }
);

下一步