Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Commit

Permalink
Add channels (#120)
Browse files Browse the repository at this point in the history
* Add channels options

* 0.4.4
  • Loading branch information
CjS77 authored and fb55 committed Oct 11, 2017
1 parent 86c2987 commit b703f64
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
15 changes: 12 additions & 3 deletions lib/clients/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WebsocketClient extends EventEmitter {
productIDs,
websocketURI = 'wss://ws-feed.gdax.com',
auth = null,
{ heartbeat = false } = {}
{ heartbeat = false, channels = null } = {}
) {
super();
this.productIDs = Utils.determineProductIDs(productIDs);
Expand All @@ -24,6 +24,7 @@ class WebsocketClient extends EventEmitter {
'Invalid or incomplete authentication credentials. You should either provide all of the secret, key and passphrase fields, or leave auth null'
);
}
this.channels = channels;
this.auth = auth || {};
this.heartbeat = heartbeat;
this.connect();
Expand Down Expand Up @@ -61,17 +62,25 @@ class WebsocketClient extends EventEmitter {
product_ids: this.productIDs,
};

if (this.channels) {
subscribeMessage.channels = this.channels;
}

// Add Signature
if (this.auth.secret) {
let sig = signRequest(this.auth, 'GET', '/users/self');
let sig = signRequest(
this.auth,
'GET',
this.channels ? '/users/self/verify' : '/users/self'
);
Object.assign(subscribeMessage, sig);
}

this.socket.send(JSON.stringify(subscribeMessage));

if (this.heartbeat) {
// send heartbeat
var heartbeatMessage = {
const heartbeatMessage = {
type: 'heartbeat',
on: true,
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gdax",
"version": "0.4.3",
"version": "0.4.4",
"author": "Coinbase",
"bugs": "https://github.com/coinbase/gdax-node/issues",
"contributors": [
Expand Down
28 changes: 28 additions & 0 deletions tests/websocket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ suite('WebsocketClient', () => {
});
});
});

test('passes channels through', done => {
const server = testserver(++port, () => {
new Gdax.WebsocketClient(
'ETH-USD',
'ws://localhost:' + port,
{
key: 'suchkey',
secret: 'suchsecret',
passphrase: 'muchpassphrase',
},
{ channels: ['user', 'ticker'] }
);
});
server.on('connection', socket => {
socket.on('message', data => {
const msg = JSON.parse(data);
assert.equal(msg.type, 'subscribe');
assert.equal(msg.key, 'suchkey');
assert.equal(msg.passphrase, 'muchpassphrase');
assert.deepEqual(msg.channels, ['user', 'ticker']);
assert(msg.timestamp);
assert(msg.signature);
server.close();
done();
});
});
});
});

test('passes heartbeat details through', done => {
Expand Down

0 comments on commit b703f64

Please sign in to comment.