From 9c66da79e5a553636cbbe3dcb6ae94b8583bde18 Mon Sep 17 00:00:00 2001 From: Gerard Moroney Date: Fri, 29 Dec 2017 18:34:27 +0000 Subject: [PATCH] Updated configuration as part of #1 to make it easier to parse/manage --- examples/src/client.js | 17 ++++++++ examples/src/server.js | 71 ++++++++++++++++++++++++++++++ examples/src/server0.js | 96 +++++++++++++++++++++++++++++++++++++++++ examples/src/server1.js | 75 ++++++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 examples/src/client.js create mode 100644 examples/src/server.js create mode 100644 examples/src/server0.js create mode 100644 examples/src/server1.js diff --git a/examples/src/client.js b/examples/src/client.js new file mode 100644 index 0000000..9864731 --- /dev/null +++ b/examples/src/client.js @@ -0,0 +1,17 @@ +/*jshint esversion: 6 */ + +var sys = require('util'); + +// Connect to Redis +var redis = require('redis'); +var client = redis.createClient('6379','127.0.0.1'); +client.on('connect', function() { + console.log('Connected to Redis Server'); +}); + +client.subscribe("BITFINEX:BTCUSD"); + +client.on("message", function(channel, message) { + //const msg = JSON.parse(message); + console.log("Message '" + message + "' on channel '" + channel + "' arrived!") +}); diff --git a/examples/src/server.js b/examples/src/server.js new file mode 100644 index 0000000..83a9fc4 --- /dev/null +++ b/examples/src/server.js @@ -0,0 +1,71 @@ +/*jshint esversion: 6 */ + +// General Libraries Needed By Application +var sys = require('util'); +var config = require('../config/default.json'); + +// Connect to Redis +var redis = require('redis'); +var client = redis.createClient(config.redis.port,config.redis.host); +client.on('connect', function() { + console.log('Connected to Redis Server'); +}); + +// Connect To Exchange +const WebSocket = require('ws') +const wss = new WebSocket('wss://api.bitfinex.com/ws/') + +// Subscribe To Channel In Exchange +var bc_queue = 'BITFINEX:BTCUSD'; +wss.onopen = () => { + console.log('Subscribing: BTCUSD'); + wss.send(JSON.stringify( + { + "event": "subscribe", + "channel": "trades", + "pair": "BTCUSD" + } + )); +} + +// Parse channel information and send to Redis +wss.onmessage = (msg) => { + //console.log(msg.data) + var resp = JSON.parse(msg.data); + var head = resp["event"]; + var head_body = resp[1]; + + //console.log(head, msg.data) + //console.log(hb); + if ( head == "subscribed" ) { + console.log( bc_queue, " channelID = ", resp["chanId"], " currency = ", resp["pair"]); + } else { + if ( resp[1] == "tu") + { + tr_timestamp=new Date(resp[4]*1000); + tr_id=resp[3]; + tr_price=resp[5]; + tr_amount=resp[6]; + tr_side=( tr_amount > 0 ? "buy" : "sell" ); + var msg = { "tr_id": tr_id, "tr_timestamp": tr_timestamp, "tr_price": tr_price, "tr_amount": tr_amount, "tr_side": tr_side }; + console.log(bc_queue, tr_id); + client.publish(bc_queue,JSON.stringify(msg)); + } + } +} + +function main () { + + // Loop through exchanges (preparation for later) + for (var i=1; i < config.exchanges.length; i++ ) + { + console.log("Exchange Name =" + config.exchanges[i]["name"]); + console.log("Exchange WSSURL =" + config.exchanges[i]["wssurl"]); + console.log("Exchange Pairs =" + config.exchanges[i]["pairs"]); + console.log("FUNCTION -> ",config.exchanges[i]["wssurl"], config.exchanges[i]["wssurl"],config.exchanges[i]["wssurl"] ) + }; + +} + +// Start application +main(); diff --git a/examples/src/server0.js b/examples/src/server0.js new file mode 100644 index 0000000..18d7d6e --- /dev/null +++ b/examples/src/server0.js @@ -0,0 +1,96 @@ +/*jshint esversion: 6 */ + +var sys = require('util'); + +const WebSocket = require('ws') +const wss = new WebSocket('wss://api.bitfinex.com/ws/') + +wss.onopen = () => { + console.log('Subscribing: BTCUSD'); + wss.send(JSON.stringify( + { + "event":"subscribe", + "channel":"book", + "pair":"BTCUSD", + "prec":"P0", + "freq":"F1" + } + )); + console.log('Subscribing: LTCUSD'); + wss.send(JSON.stringify( + { + "event":"subscribe", + "channel":"book", + "pair":"LTCUSD", + "prec":"P0", + "freq":"F1" + } + )); + console.log('Subscribing: LTCBTC'); + wss.send(JSON.stringify( + { + "event":"subscribe", + "channel":"book", + "pair":"LTCBTC", + "prec":"P0", + "freq":"F1" + } + )); + console.log('Subscribing: ETHUSD'); + wss.send(JSON.stringify( + { + "event":"subscribe", + "channel":"book", + "pair":"ETHUSD", + "prec":"P0", + "freq":"F1" + } + )); + console.log('Subscribing: ETHBTC'); + wss.send(JSON.stringify( + { + "event":"subscribe", + "channel":"book", + "pair":"ETHBTC", + "prec":"P0", + "freq":"F1" + } + )); +} + +// bitfinex - supported : BTCUSD, LTCUSD, LTCBTC, ETHUSD, ETHBTC, ETCUSD, ETCBTC, BFXUSD, BFXBTC, RRTUSD, RRTBTC, ZECUSD, ZECBTC +wss.onmessage = (msg) => { + //console.log(msg.data) + var response = JSON.parse(msg.data); + var head = response["event"]; + var head_body = response[1]; + + //console.log(head, msg.data) + //console.log(hb); + if ( head == "subscribed" ) { + console.log("channelID = ", response["chanId"], " currency = ", response["pair"]); + } else { + //console.log(msg.data); + } + //if(header != "hb" && header != "subscribed") { + //document.getElementById("btc").innerHTML = "ASK: " + response[3] + "
LAST: " + response[7] + "
BID: " + response[1]; + //console.log("ASK: " + response[3] + "
LAST: " + response[7] + "
BID: " + response[1]); + // console.log(msg.data); + //} +} + +// API keys setup here (See "Authenticated Channels") + + + + +//var WebSocket = require('websocket').WebSocket; +//var ws = new WebSocket('wss://api.bitfinex.com/ws'); + +//ws.addListener('data', function(buf) { +// sys.debug('Got data: ' + sys.inspect(buf)); +//}); + +//ws.onmessage = function(m) { +// sys.debug('Got message: ' + m); +//} diff --git a/examples/src/server1.js b/examples/src/server1.js new file mode 100644 index 0000000..0f48ee9 --- /dev/null +++ b/examples/src/server1.js @@ -0,0 +1,75 @@ +/*jshint esversion: 6 */ + +var sys = require('util'); +var redis = require('redis'); +var redis_port = "6379"; +var redis_host = "172.17.0.2"; +var client = redis.createClient(redis_port, redis_host, {no_ready_check: true}); + +client.auth('password', function (err) { + if (err) throw err; +}); + +client.on('connect', function() { + console.log('Connected to Redis'); +}); + + +const WebSocket = require('ws') +const wss = new WebSocket('wss://api.bitfinex.com/ws/') + +wss.onopen = () => { + console.log('Subscribing: BTCUSD'); + wss.send(JSON.stringify( + { + "event": "subscribe", + "channel": "trades", + "pair": "BTCUSD" + } + )); +} + +// bitfinex - supported : BTCUSD, LTCUSD, LTCBTC, ETHUSD, ETHBTC, ETCUSD, ETCBTC, BFXUSD, BFXBTC, RRTUSD, RRTBTC, ZECUSD, ZECBTC +wss.onmessage = (msg) => { + //console.log(msg.data) + var response = JSON.parse(msg.data); + var head = response["event"]; + var head_body = response[1]; + + //console.log(head, msg.data) + //console.log(hb); + if ( head == "subscribed" ) { + console.log("channelID = ", response["chanId"], " currency = ", response["pair"]); + } else { + if ( response[1] == "tu") + { + trade_timestamp=response[4]; + trade_id=response[3]; + trade_price=response[5]; + trade_amount=response[6]; + trade_side=( trade_amount > 0 ? "buy" : "sell" ); + console.log("trade_timestamp = " + trade_timestamp + " trade_id = " + trade_id + " trade_price = " + trade_price + " trade_amount = " + trade_amount + " trade_side = " + trade_side); + } + } + //if(header != "hb" && header != "subscribed") { + //document.getElementById("btc").innerHTML = "ASK: " + response[3] + "
LAST: " + response[7] + "
BID: " + response[1]; + //console.log("ASK: " + response[3] + "
LAST: " + response[7] + "
BID: " + response[1]); + // console.log(msg.data); + //} +} + +// API keys setup here (See "Authenticated Channels") + + + + +//var WebSocket = require('websocket').WebSocket; +//var ws = new WebSocket('wss://api.bitfinex.com/ws'); + +//ws.addListener('data', function(buf) { +// sys.debug('Got data: ' + sys.inspect(buf)); +//}); + +//ws.onmessage = function(m) { +// sys.debug('Got message: ' + m); +//}