Skip to content

Commit 0f58441

Browse files
author
Jon Eyrick
authored
WebSocket shouldn't instantiate at startup unless requested
1 parent 84ff854 commit 0f58441

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

node-idex-api.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module.exports = function () {
22
'use strict';
33
const WebSocket = require('ws');
44
const axios = require('axios');
5-
const socket = new WebSocket('wss://api-cluster.idex.market');
65

76
async function api(action, json = {}) {
87
const userAgent = 'Mozilla/4.0 (compatible; Node IDEX API)';
@@ -14,11 +13,11 @@ module.exports = function () {
1413
try {
1514
const response = await axios.request({
1615
url: action,
17-
method: 'POST',
1816
headers: headers,
17+
method: 'POST',
1918
baseURL: 'https://api.idex.market/'
2019
}, json);
21-
if (response && response.status !== 200) return new Error(JSON.stringify(response.data));
20+
if ( response && response.status !== 200 ) return new Error(JSON.stringify(response.data));
2221
return response.data;
2322
} catch (error) {
2423
return new Error(JSON.stringify(error.response.data));
@@ -79,10 +78,13 @@ module.exports = function () {
7978
withdraw: async function withdraw() {
8079
return await api(`withdraw`)
8180
},
82-
websockets: function websockets(ticker) {
81+
82+
// WebSocket live ticker updates
83+
subscribe: function subscribe(ticker) {
84+
const socket = new WebSocket('wss://api-cluster.idex.market');
8385
socket.on('message', message => console.log(message));
8486

85-
//ws.on('pong', handleSocketHeartbeat); // Update timer. Reconnect if it stops updating
87+
//ws.on('pong', handleSocketHeartbeat); // Update timer & reconnect zombie sockets
8688

8789
socket.on('error', error => {
8890
console.error('WebSocket error: ', error);
@@ -98,6 +100,18 @@ module.exports = function () {
98100
}
99101
});
100102
});
103+
},
104+
105+
// Convert to sortable array. {"ETHBTC":{}} to [{symbol:"ETHBTC"}]
106+
obj_to_array: json => {
107+
let output = [];
108+
for ( let key in json ) {
109+
let obj = json[key];
110+
obj.symbol = key;
111+
output.push(obj);
112+
}
113+
return output;
101114
}
115+
102116
}
103117
}();

0 commit comments

Comments
 (0)