Skip to content

Commit

Permalink
Merge pull request #1188 from starknet-io/docs/batch
Browse files Browse the repository at this point in the history
docs: batchClient
  • Loading branch information
tabaktoni authored Jul 26, 2024
2 parents e80f120 + 13ef078 commit 72c0ec0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions www/docs/guides/connect_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,43 @@ const provider = new RpcProvider({ nodeUrl: 'http://127.0.0.1:5050/rpc' });
```

> If you have customized host and port during starknet-devnet initialization, adapt in accordance your script.
## Batch JSON-RPC

The BatchClient class allows requests to be batched together in a single HTTP request, either by the interval amount or at the end of the callback queue if the batch is set to 0. By batching requests, we can reduce the overhead associated with handling individual requests.

#### Example of usage with RpcProvider

```typescript
const myProvider = new RpcProvider({
batch: 0,
});

const [getBlockResponse, blockHashAndNumber, txCount] = await Promise.all([
myBatchProvider.getBlock(),
myBatchProvider.getBlockLatestAccepted(),
myBatchProvider.getBlockTransactionCount('latest'),
]);

// ... usage of getBlockResponse, blockHashAndNumber, txCount
```

#### Example of direct usage of underlying BatchClient class

```typescript
const provider = new RpcProvider();

const batchClient = new BatchClient({
nodeUrl: provider.channel.nodeUrl,
headers: provider.channel.headers,
interval: 0,
});

const [getBlockResponse, blockHashAndNumber, txCount] = await Promise.all([
myBatchProvider.getBlock(),
myBatchProvider.getBlockLatestAccepted(),
myBatchProvider.getBlockTransactionCount('latest'),
]);

// ... usage of getBlockResponse, blockHashAndNumber, txCount
```

0 comments on commit 72c0ec0

Please sign in to comment.