> ## Documentation Index
> Fetch the complete documentation index at: https://www.helius.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 基于WebSocket的notifyOn过滤

> 在LaserStream WebSocket流中，使用accountSubscribe和programSubscribe中的notifyOn选项跳过无操作的账户更新——仅在事务实际写入账户时收到通知。

\[`notifyOn`]选项在[`accountSubscribe`](/docs/zh/api-reference/rpc/websocket/accountsubscribe)和[`programSubscribe`](/docs/zh/api-reference/rpc/websocket/programsubscribe) WebSocket方法上，让订阅跳过那些事务**写锁定但从未写入**的账户更新。同样的过滤器也可用于gRPC，详见[gRPC版本的notifyOn过滤](/docs/zh/laserstream/notify-on-filtering)。

## 问题：写锁生成重复更新

在Solana上，验证者会对每个被事务写锁定的账户发出更新通知——即使没有指令实际写入该账户，因此其余额和数据未发生变化。对于繁忙的账户，这意味着一连串重复的“无变化”通知，这会消耗带宽而不携带新信息。

## 过滤模式

`notifyOn`取两个字符串值之一：

| 值         | 发送通知时机          | 适用场景                    |
| --------- | --------------- | ----------------------- |
| `"lock"`  | 事务写锁定账户时，无论是否写入 | 默认设置——全面可见性，包括仅被事务触及的账户 |
| `"write"` | 事务实际写入账户时       | 去除无操作噪音——大多数消费者不会损失任何信息 |

值得注意的两个细节：

* **相同数据的写入仍计为写入**并被传递。`write`过滤掉仅锁定而非写入的触碰。在实践中，这些相同数据的写入很少见——在大多数情况下少于5%的更新。
* `write`丢弃的更新与您已接收的状态完全相同。如果您依赖通知作为“此账户被事务锁定”的信号（例如，活动跟踪），请坚持使用`lock`。

省略此字段的订阅行为保持不变，因此可以安全地添加到现有订阅中。

## 在`accountSubscribe`中使用

将`notifyOn`添加到配置对象中：

```javascript theme={"system"}
const ws = new WebSocket('wss://mainnet.helius-rpc.com/?api-key=<API_KEY>');

ws.on('open', () => {
  ws.send(JSON.stringify({
    jsonrpc: '2.0',
    id: 1,
    method: 'accountSubscribe',
    params: [
      '<ACCOUNT_PUBKEY>',
      {
        encoding: 'jsonParsed',
        commitment: 'confirmed',
        notifyOn: 'write' // skip no-op updates
      }
    ]
  }));
  setInterval(() => ws.ping(), 30_000);
});

ws.on('message', (data) => {
  const msg = JSON.parse(data.toString());
  if (msg.params?.result) console.log(msg.params.result);
});
```

## 在`programSubscribe`中使用

相同的键适用于`programSubscribe`配置对象，过滤掉每个程序所拥有账户的无操作更新：

```javascript theme={"system"}
ws.send(JSON.stringify({
  jsonrpc: '2.0',
  id: 1,
  method: 'programSubscribe',
  params: [
    '<PROGRAM_ID>',
    {
      encoding: 'jsonParsed',
      commitment: 'confirmed',
      filters: [{ dataSize: 165 }],
      notifyOn: 'write' // skip no-op updates
    }
  ]
}));
```

解析是故障开放的：只有`"write"`（不区分大小写）选择加入。任何其他情况——缺失、拼写错误、未知标记——都会解析为`lock`，因此错误会让您保持在所有更新路径上，而不是出现错误或默默丢弃数据。

## 相关内容

<CardGroup cols={2}>
  <Card title="accountSubscribe" icon="bolt" href="/docs/zh/api-reference/rpc/websocket/accountsubscribe">
    完整的`accountSubscribe`方法参考，包括`notifyOn`。
  </Card>

  <Card title="programSubscribe" icon="code" href="/docs/zh/api-reference/rpc/websocket/programsubscribe">
    完整的`programSubscribe`方法参考，包括`notifyOn`。
  </Card>

  <Card title="notifyOn Filtering (gRPC)" icon="filter" href="/docs/zh/laserstream/notify-on-filtering">
    LaserStream gRPC账户过滤的相同过滤器。
  </Card>

  <Card title="如何使用accountSubscribe" icon="wallet" href="/docs/zh/rpc/websocket/account-subscribe">
    通过WebSocket流式传输账户更新的指南。
  </Card>
</CardGroup>
