MT4 manager gRPC NodeJS example
Table of Contents
Run install packages installation:
npm install
Input your server credentials
This example has a dynamic code generation
You can find satic code generation in the typescript example.
Run an example:
node app.js
The script:
'use strict';
const PROTO_PATH = __dirname + '/proto/mt4mng.proto';
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const async = require('async');
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const mng4grpc = grpc.loadPackageDefinition(packageDefinition).mng4grpc;
const mainControllerClient = new mng4grpc.MainController(
"mng4grpc.mtapi.io:443",
grpc.credentials.createSsl()
);
function main() {
async.waterfall([
connectToServer,
getAccounts,
getIndividualAccountDataForEachAccount,
]);
}
function getIndividualAccountDataForEachAccount(token, accountIds, callback) {
accountIds.forEach((accountId) => {
mainControllerClient.accountDetails({
id: token,
login: accountId
},(err, value) => {
console.log('Account id:', accountId);
if (err) {
callback(err)
console.error(err.message);
} else {
if (value?.error) console.log(value?.error.message);
else {
if (value?.result) {
let accountDetails = 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);
}
}
}
})
});
}
function getAccounts(token, callback) {
mainControllerClient.AccountsList(
{
id: token
},
(err, value) => {
if (err) {
callback(err)
console.error(err.message);
} else {
if (value?.error) console.log(value?.error.message);
else {
if (value?.result) {
callback(null, token, value?.result)
}
}
}
}
)
}
function connectToServer(callback) {
mainControllerClient.Connect({
server: '',
user: 1,
password: '',
}, (err, value) => {
console.log('Connection processing');
if (err) {
callback(err)
console.error(err.message);
} else {
if (value?.error) console.log(value?.error.message);
else {
if (value?.result) {
console.log('Connection token:', value?.result);
callback(null, value?.result)
}
}
}
})
}
if (require.main === module) {
main();
}