Skip to content

Instantly share code, notes, and snippets.

@sharife3
Last active October 19, 2020 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharife3/9136dd6e5e36e88ba2615333189ae23f to your computer and use it in GitHub Desktop.
Save sharife3/9136dd6e5e36e88ba2615333189ae23f to your computer and use it in GitHub Desktop.
Example of connecting and consuming APEX:E3 Data
// yarn or npm install dependencies
const WebSocket = require("ws");
const fetch = require("node-fetch");
const clientId = "[CLIENT_ID]";
const clientSecret = "[CLIENT_SECRET]";
const authUrl = `https://keycloak.ae3platform.com/auth/realms/ApexE3/protocol/openid-connect/token`;
const wsUrl = `wss://ws.ae3platform.com/`;
(async () => {
console.log(`--------- Authenticating ---------\n\n`);
const data = new URLSearchParams();
data.append("grant_type", "client_credentials");
data.append("client_id", clientId);
data.append("client_secret", clientSecret);
const authRespose = await fetch(authUrl, {
method: "POST",
body: data,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Origin: "*",
},
});
const accessToken = await authRespose.json().then((v) => v.access_token);
console.log(`--------- Authentication Token ---------\n\n`);
console.log(accessToken);
console.log("\n\n");
const url = `${wsUrl}?token=${accessToken}`;
const connection = new WebSocket(url);
connection.onopen = () => {
console.log(`--------- WebSocket Opened ---------\n\n`);
const subscriptionRequest = {
action: "SUBSCRIBE",
data: {
event: "ORDERBOOK",
baseId: "BTC:CRYPTO",
quoteId: "USDT:CRYPTO",
exchangeId: ["OKEX", "HITBTC", "BITFINEX", "BINANCE", "GATEIO", "FTX", "BITTREX", "HUOBIPRO", "KRAKEN", "POLONIEX", "ZB"],
marketType: "SPOT",
assetClassification: "ALT",
},
};
console.log(`--------- Subscription Request ---------\n\n`);
console.log(subscriptionRequest);
console.log("\n\n");
};
connection.onerror = (error) => {
console.log(`WebSocket error:`, error);
};
connection.onmessage = (e) => {
console.log(`\n\n--------- WebSocket Message ---------\n\n`);
console.log(e.data);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment