SDK best practices#
To ensure optimal reliability when using our SDK, please adhere to the following guidelines regarding instance management. Following them will help to reduce server load and minimize the risk of rate-limiting issues.
Examples in this document are represented for the JavaScript SDK, but the ideas are the same for any other language.
Avoid creating short-living SDK instances#
The SDK is intended and optimized to be long-living, reusing internal socket connections to different accounts and some other state, so it is recommended to cache and reuse a single SDK instance throughout your application's lifecycle when possible.
If temporary short-living instances are unavoidable, ensure they are properly closed.
Managing account synchronizations and historical requests#
Processes, related to account synchronizations and retrieving historical market data, may be heavy and take a long time, so they are subject to rate limiting. Therefore, in order to decrease probability of hitting a rate limit, we recommend to throttle concurrent initial account synchronizations and historical data requests.
Proper resource cleanup#
To prevent resource leaks and unnecessary connection overhead:
Account connections#
Ensure unneded anymore account connections are properly closed, by calling close()
method on them:
const account = await api.metatraderAccountApi.getAccount('accountId');
const connection = account.getStreamingConnection();
await connection.connect();
// ... use the connection ...
await connection.close(); // release the connection
This is related to both - RPC and streaming connections.
SDK instances#
Call the close()
method when an SDK instance is no longer needed:
const api = new MetaApi(token);
// ... use the SDK ...
api.close(); // close the SDK