> ## 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.

# accountSubscribeの使用方法

> `accountSubscribe`を使ってリアルタイムでSolanaのアカウント更新をストリームします。WebSocketを介してバランスの変化、データ変更、およびラートの更新を監視します。

## `accountSubscribe`とは何ですか？

SolanaのWebSocketsは、アカウントに購読し、対応するアカウントの公開鍵に関連するラートやデータが変更された際にWebSocket接続を通じて通知を受け取ることができる方法をサポートしています。

この方法は、[Solana WSS API仕様](https://solana.com/docs/rpc/websocket#accountsubscribe)に直接対応しています。

## パラメータ

* `string`: アカウントの公開鍵、`base58`形式で送信されます（必須）
* `object`: 追加のパラメータを渡すために使用されるオプションのオブジェクト
* `encoding`: `AccountNotification`で返されるデータの形式を指定します。サポートされている値: `base58`（デフォルト）、`base64`、`base64+zstd`、`jsonParsed`
* `commitment`: トランザクションのコミットメントレベルを定義します。サポートされている値: `finalized`（デフォルト）、`confirmed`、`processed`
* `notifyOn`: **廃止 — Agave 4.2以降無操作。** `notifyOn`を設定しても効果はありません。このフィールドは後日削除される予定です。

## アカウント購読例

この例では、アカウント`SysvarC1ock11111111111111111111111111111111`のアカウント変更を購読しています。

このアカウントのデータまたはラートに変化があると更新が見られます。

この特定のアカウントでは、`slot`と`unixTimestamp`の両方が返されるアカウントデータの一部であるため、頻繁に発生します。

<Tip>
  Heliusのフィルターされた`accountSubscribe`は、他のWebSocket購読方法と同じ統一された`wss://mainnet.helius-rpc.com`および`wss://devnet.helius-rpc.com` [エンドポイント](https://www.helius.dev/docs/api-reference/endpoints)上に存在します。
</Tip>

<CodeGroup>
  ```javascript theme={"system"}
  const WebSocket = require('ws');

  // Create a WebSocket connection
  const ws = new WebSocket('wss://mainnet.helius-rpc.com?api-key=<API_KEY>');

  // Function to send a request to the WebSocket server
  function sendRequest(ws) {
      const request = {
          jsonrpc: "2.0",
          id: 420,
          method: "accountSubscribe",
          params: [
              "SysvarC1ock11111111111111111111111111111111", // pubkey of account we want to subscribe to
              {
                  encoding: "jsonParsed", // base58, base64, base64+zstd, jsonParsed
                  commitment: "confirmed", // defaults to finalized if unset
              }
          ]
      };
      ws.send(JSON.stringify(request));
  }

  // Function to send a ping to the WebSocket server
  function startPing(ws) {
      setInterval(() => {
          if (ws.readyState === WebSocket.OPEN) {
              ws.ping();
              console.log('Ping sent');
          }
      }, 30000); // Ping every 30 seconds
  }

  // Define WebSocket event handlers

  ws.on('open', function open() {
      console.log('WebSocket is open');
      sendRequest(ws);  // Send a request once the WebSocket is open
      startPing(ws);    // Start sending pings
  });

  ws.on('message', function incoming(data) {
      const messageStr = data.toString('utf8');
      try {
          const messageObj = JSON.parse(messageStr);
          console.log('Received:', messageObj);
      } catch (e) {
          console.error('Failed to parse JSON:', e);
      }
  });

  ws.on('error', function error(err) {
      console.error('WebSocket error:', err);
  });

  ws.on('close', function close() {
      console.log('WebSocket is closed');
  });
  ```
</CodeGroup>

### 通知の例

<CodeGroup>
  ```json theme={"system"}
  {
      "jsonrpc": "2.0",
      "method": "accountNotification",
      "params": {
          "subscription": 237508762798666,
          "result": {
              "context": {"slot": 235781083},
              "value": {
                  "lamports": 1169280,
                  "data": "BvEhEb6hixL3QPn41gHcyi2CDGKt381jbNKFFCQr6XDTzCTXCuSUG9D",
                  "owner": "Sysvar1111111111111111111111111111111111111",
                  "executable": false,
                  "rentEpoch": 361,
                  "space": 40
              }
          }
      }
  }
  ```
</CodeGroup>
