> For the complete documentation index, see [llms.txt](https://docs.zeta.markets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zeta.markets/build-with-zeta/sdks/typescript-sdk/examples/subscription.md).

# Subscription

***Github repository:*** [***https://github.com/zetamarkets/sdk/tree/main/examples/subscription***](https://github.com/zetamarkets/sdk/tree/main/examples/subscription)

Subscribing to changes to onchain is easy using `subscription.subscribeProgramAccounts`. In this example we subscribe to any SOL margin account changes, but you can subscribe to any account types you wish to track.

<details>

<summary>Code</summary>

```typescript
require("dotenv").config();

import {
  Exchange,
  Network,
  utils,
  types,
  programTypes,
  subscription,
  constants,
} from "@zetamarkets/sdk";

import { PublicKey, Connection } from "@solana/web3.js";

const NETWORK_URL = process.env["network_url"]!;

let network: Network;

switch (process.env["network"]) {
  case "localnet":
    network = Network.LOCALNET;
    break;
  case "devnet":
    network = Network.DEVNET;
    break;
  case "mainnet":
    network = Network.MAINNET;
    break;
  default:
    throw Error("Unsupported network type!");
}

async function main() {
  // Create a solana web3 connection to devnet.
  const connection = new Connection(NETWORK_URL, "confirmed");

  const loadExchangeConfig = types.defaultLoadExchangeConfig(
    network,
    connection,
    utils.defaultCommitment(),
    0, // ThrottleMs - increase if you are running into rate limit issues on startup.
    true
  );

  await Exchange.load(
    loadExchangeConfig,
    // Exchange wallet can be ignored for normal clients.
    undefined
  );

  subscription.subscribeProgramAccounts<programTypes.CrossMarginAccount>(
    types.ProgramAccountType.CrossMarginAccount,
    async (
      data: subscription.AccountSubscriptionData<programTypes.CrossMarginAccount>
    ) => {
      // Here you can filter for only accounts you care about (such as your own)
      console.log(data);
    }
  );

  await utils.sleep(100_000);

  // Close to end the websockets.
  // await Exchange.close();
}

main().catch(console.error.bind(console));
```

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.zeta.markets/build-with-zeta/sdks/typescript-sdk/examples/subscription.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
