MT4 manager gRPC JavaScript TypeScript client example

Created with Sketch.

MT4 manager gRPC JavaScript TypeScript client example

Table of Contents

API methods browser

MT4 manager proto file

You need npm manager

Ready to run example

You need auto generated mt4 manager client libraries to run the code. The example has a script named "build:proto". This script creates libraries for you, and you should't care about this.

But, for those of you who has more experience and, for some reason, wants to generate libraries separately or download it directly, we provide direct link and generation script.

Direct download link for js typescript autogenerated libraries.

The script of generation typescript.
Windows:

docker run -v ${pwd}:/defs namely/protoc-all -f ./proto/*.proto -l typescript -o generated --ts_opt useOptionals=all

Linux:

docker run --user "$(id -u):$(id -g)" -v `pwd`:/defs namely/protoc-all -f ./proto/*.proto -l typescript -o generated --ts_opt useOptionals=all

Let's return to our ready to run example.

Run install packages

npm install

Run proto stubs building

npm run build:proto

Set your mt4 server credetials

Run build sources script

npm run build

Run mt4 manager grpc client

npm run start:client

import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';

import { ProtoGrpcType } from './proto/mt4mng';
import { MainControllerClient } from './proto/mng4grpc/MainController';
import { AccountsReply } from './proto/mng4grpc/AccountsReply';
import { UserRecord__Output } from './proto/mng4grpc/UserRecord';

const host = 'mng4grpc.mtapi.io:443';

const packageDefinition = protoLoader.loadSync('./proto/mt4mng.proto');
const proto = grpc.loadPackageDefinition(
  packageDefinition
) as unknown as ProtoGrpcType;

const mainControllerClient: MainControllerClient = new proto.mng4grpc.MainController(
  host,
  grpc.credentials.createSsl()
);

const deadline = new Date();
deadline.setSeconds(deadline.getSeconds() + 5);

mainControllerClient.waitForReady(deadline, (error?: Error) => {
  if (error) {
    console.log(`Client connect error: ${error.message}`);
  } else {
    onMainControllerReady();
  }
});

function onMainControllerReady() {
  mainControllerClient.Connect(
    {
      server: '',
      user: 1,
      password: '',
    }, (err, value) => {
      if (err) {
        console.error(err.message);
      } else {
        if (value?.error) console.log(value?.error.message);
        else {
          if (value?.result) {
            console.log('Connection token:', value?.result);
            OnConnectionEstablished(value?.result);
          }
        }
      }
    }
  )
}

function OnConnectionEstablished(token: string) {
  mainControllerClient.AccountsList(
    {
      id: token
    },
    (err, value) => {
      if (err) {
        console.error(err.message);
      } else {
        if (value?.error) console.log(value?.error.message);
        else {
          if (value?.result) {
            let accountIds : number[] = value?.result;
            accountIds.forEach((accountId) => GetAccountProperties(accountId, token));
          }
        }
      }
    }
  )
}

function GetAccountProperties(accountId: number, token: string) {
  mainControllerClient.accountDetails({
    id: token,
    login: accountId
  },(err, value) => {
    console.log('Account id:', accountId);
    if (err) {
      console.error(err.message);
    } else {
      if (value?.error) console.log(value?.error.message);
      else {
        if (value?.result) {
          let accountDetails : UserRecord__Output = value?.result;
          console.log('Account zip code:', accountDetails.Zipcode);
          console.log('Account Name:', accountDetails.Name);
          console.log('Account Balance:', accountDetails.Balance);
          console.log('Account State:', accountDetails.State);
        }
      }
    }
  })
}

Leave a Reply

Your email address will not be published. Required fields are marked *