· 6 years ago · Oct 11, 2019, 06:14 AM
1// Initialise Kafka
2var kafka = require('kafka-node'),
3 Producer = kafka.Producer,
4 client = new kafka.KafkaClient({kafkaHost: 'kafka:9092'}),
5 producer = new Producer(client)
6 KeyedMessage = kafka.KeyedMessage
7
8// Import database Interactions
9const {
10 bitmexKeysAll
11} = require('../../utils/db')
12
13// Set module exports to exports to make the exporting cleaner
14var exports = module.exports = {};
15
16// Set up the delay
17const delay = ms => new Promise(res => setTimeout(res, ms));
18
19
20// TODO : Use the 1m trade bin for BTC
21//Open a websocket to bitmex to retrieve the trade bins for the speecified asset and the specified timeframe
22exports.btc1mTradeBin = async () => {
23 // Retrive all of the API keys for each of the bots
24 let APIkeys = await bitmexKeysAll()
25
26 // Asign to a var the object for the first api key
27 // We realy only need one API key in this instance so no need to loop through al of them
28 let bitmexAPI = APIkeys[0].key
29
30 // Get the position from the bitmex websocket
31 let btc1m = new Promise(async function(resolve, reject) {
32 await bitmexAPI.monitorStream('XBTUSD', 'tradeBin1m', (data, err) =>{
33
34 // Data
35 let priceBin = JSON.stringify(data)
36
37 // Set up the payload Topic Message and Partition
38 payloads = [
39 { topic: 'price', messages: priceBin , partition: 0 },
40 ],
41
42 // Send the message to the Kafka Broker
43 producer.send(payloads, function (err, res) {z
44 })
45
46 })
47 resolve()
48 })
49 btc1m.then(successCallback,failureCallback)
50}
51
52function successCallback(result) {
53 console.log("Audio file ready at URL: " + result);
54 }
55
56 function failureCallback(error) {
57 console.error("Error generating audio file: " + error);
58 }
59
60
61
62
63
64
65
66
67 WebSocket [ERROR]: Unexpected response from server [429]: {"error":"Rate limit exceeded, retry in 80 seconds."}
68WebSocket [INFO]: The WebSocket will terminate. Please manually reconnect.
69WebSocket [ERROR]: WebSocket closed unexpectedly.
70/usr/src/app/node_modules/bitmex-realtime-api/lib/createSocket.js:70
71 if (!listeners.length) throw new Error('WebSocket closed. Please check errors above.');
72 ^
73
74Error: WebSocket closed. Please check errors above.
75 at WebSocketClient.wsClient.onend (/usr/src/app/node_modules/bitmex-realtime-api/lib/createSocket.js:70:34)
76 at WebSocket.instance.on (/usr/src/app/node_modules/bitmex-realtime-api/lib/ReconnectingSocket.js:45:12)
77 at WebSocket.emit (events.js:193:13)
78 at closeConnection (/usr/src/app/node_modules/bitmex-realtime-api/lib/ReconnectingSocket.js:71:23)
79 at IncomingMessage.response.on (/usr/src/app/node_modules/bitmex-realtime-api/lib/ReconnectingSocket.js:84:9)
80 at IncomingMessage.emit (events.js:198:15)
81 at endReadableNT (_stream_readable.js:1139:12)
82 at processTicksAndRejections (internal/process/task_queues.js:81:17)
83error Command failed with exit code 1.