Skip to content

Commit

Permalink
Updated configuration as part of #1 to make it easier to parse/manage
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerard Moroney committed Dec 29, 2017
1 parent 4270e08 commit 9c66da7
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/src/client.js
Original file line number Diff line number Diff line change
@@ -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!")
});
71 changes: 71 additions & 0 deletions examples/src/server.js
Original file line number Diff line number Diff line change
@@ -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();
96 changes: 96 additions & 0 deletions examples/src/server0.js
Original file line number Diff line number Diff line change
@@ -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] + "<br> LAST: " + response[7] + "<br> BID: " + response[1];
//console.log("ASK: " + response[3] + "<br> LAST: " + response[7] + "<br> 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);
//}
75 changes: 75 additions & 0 deletions examples/src/server1.js
Original file line number Diff line number Diff line change
@@ -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] + "<br> LAST: " + response[7] + "<br> BID: " + response[1];
//console.log("ASK: " + response[3] + "<br> LAST: " + response[7] + "<br> 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);
//}

0 comments on commit 9c66da7

Please sign in to comment.