Comparing version 0.3.0 to 0.3.1
@@ -9,9 +9,9 @@ var EventEmitter = require('events').EventEmitter; | ||
* Create a new connection to a websocket feed | ||
* @param productID {string} Options. The GDAX product to connect to. Default: 'BTC-USD' | ||
* @param productIDs {array} The GDAX products to listen to. Default: ['BTC-USD'] | ||
* @param websocketURI {string} Optional. The websocker URL. Default: The official GDAX feed. | ||
* @param auth {object} Optional. An object containing your API ket details (key, secret & passphrase) | ||
*/ | ||
var WebsocketClient = function(productID, websocketURI, auth) { | ||
var WebsocketClient = function(productIDs, websocketURI, auth) { | ||
var self = this; | ||
self.productID = productID || 'BTC-USD'; | ||
self.productIDs = self._determineProductIDs(productIDs); | ||
self.websocketURI = websocketURI || 'wss://ws-feed.gdax.com'; | ||
@@ -43,2 +43,3 @@ if (auth && !(auth.secret && auth.key && auth.passphrase)) { | ||
self.socket.on('close', self.onClose.bind(self)); | ||
self.socket.on('error', self.onError.bind(self)); | ||
}; | ||
@@ -59,12 +60,17 @@ | ||
self.emit('open'); | ||
var sig = signRequest(self.auth, 'GET', '/users/self'); | ||
var subscribeMessage = { | ||
type: 'subscribe', | ||
product_id: self.productID, | ||
signature: sig.signature, | ||
key: sig.key, | ||
passphrase: sig.passphrase, | ||
timestamp: sig.timestamp | ||
product_ids: self.productIDs | ||
}; | ||
// Add Signature | ||
if (self.auth.secret) { | ||
var sig = signRequest(self.auth, 'GET', '/users/self'); | ||
subscribeMessage.signature = sig.signature | ||
subscribeMessage.key = sig.key | ||
subscribeMessage.passphrase = sig.passphrase | ||
subscribeMessage.timestamp = sig.timestamp | ||
} | ||
self.socket.send(JSON.stringify(subscribeMessage)); | ||
@@ -91,3 +97,32 @@ | ||
prototype.onError = function(err) { | ||
var self = this; | ||
if (!err) { | ||
return; | ||
} | ||
if (err.message === 'unexpected server response (429)') { | ||
err = new Error('You are connecting too fast and are being throttled! Make sure you subscribe to multiple books on one connection.'); | ||
throw err; | ||
} | ||
self.emit('error', err); | ||
}; | ||
prototype._determineProductIDs = function(productIDs) { | ||
if (!productIDs || !productIDs.length) { | ||
return ['BTC-USD']; | ||
} | ||
if (Array.isArray(productIDs)) { | ||
return productIDs; | ||
} | ||
// If we got this far, it means it's a string. | ||
// Return an array for backwards compatibility. | ||
return [productIDs]; | ||
} | ||
}); | ||
module.exports = exports = WebsocketClient; |
{ | ||
"name": "gdax", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"author": "Coinbase", | ||
@@ -5,0 +5,0 @@ "bugs": "https://github.com/coinbase/gdax-node/issues", |
@@ -1,2 +0,2 @@ | ||
# GDAX | ||
# GDAX [![CircleCI](https://circleci.com/gh/coinbase/gdax-node.svg?style=svg)](https://circleci.com/gh/coinbase/gdax-node) | ||
The official Node.js library for the [GDAX | ||
@@ -278,3 +278,3 @@ API](https://docs.gdax.com/) (formerly Coinbase Exchange). | ||
var Gdax = require('gdax'); | ||
var websocket = new Gdax.WebsocketClient(); | ||
var websocket = new Gdax.WebsocketClient(['BTC-USD', 'ETH-USD']); | ||
websocket.on('message', function(data) { console.log(data); }); | ||
@@ -286,2 +286,3 @@ ``` | ||
* `close` | ||
* `error` | ||
@@ -288,0 +289,0 @@ ### Orderbook |
61679
18
1433
320