This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 313
Heartbeat #76
Merged
Merged
Heartbeat #76
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e6e1904
Added heartbeat option
34eb2a4
Added heartbeat option
a58125d
fix OrderbookSync test
amejiarosario 0ce5e15
fix websocket tests
amejiarosario 8c43fa5
update docs
amejiarosario 2d32d60
remove default parameter
amejiarosario 9c41071
new test
amejiarosario dfdeece
cleanup
amejiarosario 37432cb
rebase with upstream master
amejiarosario f96d156
makes it backward compatible
amejiarosario 373240b
improve tests and readme
amejiarosario File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,17 +8,21 @@ var signRequest = require('../../lib/request_signer').signRequest; | |
/** | ||
* Create a new connection to a websocket feed | ||
* @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) | ||
* @param options {object} Optional. It can contain auth, websocketURI and heartbeat | ||
* - auth {object} Optional. An object containing your API ket details (key, secret & passphrase) | ||
* - websocketURI {string} Optional. The websocker URL. Default: The official GDAX feed. | ||
* - heartbeat {boolean} Optional. Receive heartbeat messages once a second | ||
*/ | ||
var WebsocketClient = function(productIDs, websocketURI, auth) { | ||
var WebsocketClient = function(productIDs, options) { | ||
var self = this; | ||
options = options || {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to the breaking changes in the way the For example, maybe checking to ensure that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the only case either, just a thought of one potential. The idea is to look at this and ensure it is as backwards compatible friendly as possible, without making it mess. |
||
self.productIDs = Utils.determineProductIDs(productIDs); | ||
self.websocketURI = websocketURI || 'wss://ws-feed.gdax.com'; | ||
if (auth && !(auth.secret && auth.key && auth.passphrase)) { | ||
self.websocketURI = options.websocketURI || 'wss://ws-feed.gdax.com'; | ||
if (options.auth && !(options.auth.secret && options.auth.key && options.auth.passphrase)) { | ||
throw new Error('Invalid or incomplete authentication credentials. You should either provide all of the secret, key and passphrase fields, or leave auth null'); | ||
} | ||
self.auth = auth || {}; | ||
self.auth = options.auth || {}; | ||
self.heartbeat = options.heartbeat || false; | ||
EventEmitter.call(self); | ||
self.connect(); | ||
}; | ||
|
@@ -73,11 +77,30 @@ _.assign(WebsocketClient.prototype, new function() { | |
|
||
self.socket.send(JSON.stringify(subscribeMessage)); | ||
|
||
// Set a 30 second ping to keep connection alive | ||
self.pinger = setInterval(function() { | ||
self.socket.ping("keepalive"); | ||
}, 30000); | ||
|
||
// Add heartbeat option | ||
if(self.heartbeat) { | ||
var heartbeatMessage = { | ||
type: 'heartbeat', | ||
on: true | ||
}; | ||
|
||
// Add Signature | ||
if (self.auth.secret) { | ||
var sig = signRequest(self.auth, 'GET', '/users/self'); | ||
heartbeatMessage.signature = sig.signature | ||
heartbeatMessage.key = sig.key | ||
heartbeatMessage.passphrase = sig.passphrase | ||
heartbeatMessage.timestamp = sig.timestamp | ||
} | ||
|
||
self.socket.send(JSON.stringify(heartbeatMessage)); | ||
} else { | ||
// Set a 30 second ping to keep connection alive | ||
self.pinger = setInterval(function () { | ||
self.socket.ping("keepalive"); | ||
}, 30000); | ||
} | ||
|
||
}; | ||
|
||
prototype.onClose = function() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it would be good to expand upon the optional parameters (websocket URI, authentication) just so that people know more without having to dig into the code themselves to figure out how it works, or why their old implementation is breaking if they've upgraded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I'll update