Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactorized WebSocket API. #6

Merged
merged 13 commits into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.idea
/test/test_api_keys.json
/build
/build
npm-debug.log
239 changes: 239 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<a name="BitfinexWS"></a>
## BitfinexWS
**Kind**: global class

* [BitfinexWS](#BitfinexWS)
* [new BitfinexWS(APIKey, APISecret)](#new_BitfinexWS_new)
* _instance_
* [.subscribeOrderBook([pair], [precision], [length])](#BitfinexWS+subscribeOrderBook)
* [.subscribeTrades([pair])](#BitfinexWS+subscribeTrades)
* [.subscribeTicker([pair])](#BitfinexWS+subscribeTicker)
* [.unsubscribe(chanId)](#BitfinexWS+unsubscribe)
* [.auth()](#BitfinexWS+auth)
* ["message"](#BitfinexWS+event_message)
* ["open"](#BitfinexWS+event_open)
* ["error"](#BitfinexWS+event_error)
* ["close"](#BitfinexWS+event_close)
* ["subscribed"](#BitfinexWS+event_subscribed)
* ["auth"](#BitfinexWS+event_auth)
* ["ps"](#BitfinexWS+event_ps)
* ["pn"](#BitfinexWS+event_pn)
* ["pu"](#BitfinexWS+event_pu)
* ["pc"](#BitfinexWS+event_pc)
* ["ws"](#BitfinexWS+event_ws)
* ["ws"](#BitfinexWS+event_ws)
* ["os"](#BitfinexWS+event_os)
* ["on"](#BitfinexWS+event_on)
* ["ou"](#BitfinexWS+event_ou)
* ["oc"](#BitfinexWS+event_oc)
* ["te"](#BitfinexWS+event_te)
* ["tu"](#BitfinexWS+event_tu)
* ["ticker"](#BitfinexWS+event_ticker)
* ["trade"](#BitfinexWS+event_trade)
* ["orderbook"](#BitfinexWS+event_orderbook)
* _static_
* [.WebSocketURI](#BitfinexWS.WebSocketURI) : <code>String</code>

<a name="new_BitfinexWS_new"></a>
### new BitfinexWS(APIKey, APISecret)
Handles communitaction with Bitfinex WebSocket API.


| Param | Type |
| --- | --- |
| APIKey | <code>sting</code> |
| APISecret | <code>string</code> |

<a name="BitfinexWS+subscribeOrderBook"></a>
### bitfinexWS.subscribeOrderBook([pair], [precision], [length])
Subscribe to Order book updates. Snapshot will be sended as multiple updates.
Event will be emited as `PAIRNAME_book`.

**Kind**: instance method of <code>[BitfinexWS](#BitfinexWS)</code>
**See**: http://docs.bitfinex.com/#order-books

| Param | Type | Description |
| --- | --- | --- |
| [pair] | <code>string</code> | BTCUSD, LTCUSD or LTCBTC. Default BTCUSD |
| [precision] | <code>string</code> | Level of price aggregation (P0, P1, P2, P3). The default is P0. |
| [length] | <code>string</code> | Number of price points. 25 (default) or 100. |

<a name="BitfinexWS+subscribeTrades"></a>
### bitfinexWS.subscribeTrades([pair])
Subscribe to trades. Snapshot will be sended as multiple updates.
Event will be emited as `PAIRNAME_trades`.

**Kind**: instance method of <code>[BitfinexWS](#BitfinexWS)</code>
**See**: http://docs.bitfinex.com/#trades75

| Param | Type | Description |
| --- | --- | --- |
| [pair] | <code>string</code> | BTCUSD, LTCUSD or LTCBTC. Default BTCUSD |

<a name="BitfinexWS+subscribeTicker"></a>
### bitfinexWS.subscribeTicker([pair])
Subscribe to ticker updates. The ticker is a high level overview of the state
of the market. It shows you the current best bid and ask, as well as the last
trade price.

Event will be emited as `PAIRNAME_ticker`.

**Kind**: instance method of <code>[BitfinexWS](#BitfinexWS)</code>
**See**: http://docs.bitfinex.com/#ticker76

| Param | Type | Description |
| --- | --- | --- |
| [pair] | <code>string</code> | BTCUSD, LTCUSD or LTCBTC. Default BTCUSD |

<a name="BitfinexWS+unsubscribe"></a>
### bitfinexWS.unsubscribe(chanId)
Unsubscribe to a channel.

**Kind**: instance method of <code>[BitfinexWS](#BitfinexWS)</code>

| Param | Type | Description |
| --- | --- | --- |
| chanId | <code>number</code> | ID of the channel received on `subscribed` event. |

<a name="BitfinexWS+auth"></a>
### bitfinexWS.auth()
Autenticate the user. Will receive executed traded updates.

**Kind**: instance method of <code>[BitfinexWS](#BitfinexWS)</code>
**See**: http://docs.bitfinex.com/#wallet-updates
<a name="BitfinexWS+event_message"></a>
### "message"
**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_open"></a>
### "open"
WebSocket connection is open. Ready to send.

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_error"></a>
### "error"
**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_close"></a>
### "close"
WebSocket connection is closed.

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_subscribed"></a>
### "subscribed"
**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| channel | <code>string</code> | Channel type |
| pair | <code>string</code> | Currency pair. |
| chanId | <code>number</code> | Channel ID sended by Bitfinex |

<a name="BitfinexWS+event_auth"></a>
### "auth"
**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_ps"></a>
### "ps"
position snapshot

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_pn"></a>
### "pn"
new position

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_pu"></a>
### "pu"
position update

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_pc"></a>
### "pc"
position close

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_ws"></a>
### "ws"
wallet snapshot

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_ws"></a>
### "ws"
wallet snapshot

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_os"></a>
### "os"
order snapshot

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_on"></a>
### "on"
new order

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_ou"></a>
### "ou"
order update

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_oc"></a>
### "oc"
order cancel

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_te"></a>
### "te"
trade executed

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_tu"></a>
### "tu"
trade execution update

**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
<a name="BitfinexWS+event_ticker"></a>
### "ticker"
**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
**Properties**

| Name | Type |
| --- | --- |
| bid | <code>number</code> |
| bidSize | <code>number</code> |
| ask | <code>number</code> |
| askSize | <code>number</code> |
| dailyChange | <code>number</code> |
| dailyChangePerc | <code>number</code> |
| lastPrice | <code>number</code> |
| volume | <code>number</code> |
| high | <code>number</code> |
| low | <code>number</code> |

<a name="BitfinexWS+event_trade"></a>
### "trade"
**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
**See**: http://docs.bitfinex.com/#trades75
**Properties**

| Name | Type |
| --- | --- |
| seq | <code>string</code> |
| timestamp | <code>number</code> |
| price | <code>number</code> |
| amount | <code>number</code> |

<a name="BitfinexWS+event_orderbook"></a>
### "orderbook"
**Kind**: event emitted by <code>[BitfinexWS](#BitfinexWS)</code>
**See**: http://docs.bitfinex.com/#order-books
**Properties**

| Name | Type |
| --- | --- |
| price | <code>string</code> |
| count | <code>number</code> |
| amount | <code>number</code> |

<a name="BitfinexWS.WebSocketURI"></a>
### BitfinexWS.WebSocketURI : <code>String</code>
**Kind**: static constant of <code>[BitfinexWS](#BitfinexWS)</code>
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ Bitfinex API Node Reference Library
A Node.js reference implementation of the Bitfinex API. See the full docs at <http://bitfinexcom.github.io/bitfinex-api-node/>

## Installation
```
```bash
npm install bitfinex-api-node
```

## Usage
```
var bitfinex-api-node = require('bitfinex-api-node')
bitfinex-websocket = bitfinex-api-node.websocket,
bitfinex-rest = bitfinex-api-node.rest;
```js
var bitfinex-api-node = require('bitfinex-api-node')
bitfinex-websocket = bitfinex-api-node.websocket,
bitfinex-rest = bitfinex-api-node.rest;

console.log('bitfinex-api-node', bitfinex-api-node, 'bitfinex-websocket', bitfinex-websocket, 'bitfinex-rest', bitfinex-rest);
console.log('bitfinex-api-node', bitfinex-api-node, 'bitfinex-websocket',
bitfinex-websocket, 'bitfinex-rest', bitfinex-rest);
```

## Tests

```
npm test
```bash
npm test
```

## Contributing
Expand All @@ -35,4 +36,4 @@ Add unit tests for any new or changed functionality. Lint and test your code.
## Release History
```
* 0.0.1 Initial release
```
```
27 changes: 27 additions & 0 deletions examples/websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var BitfinexWS = require ('bitfinex-api-node').WS;

var bws = new BitfinexWS();

bws.on('open', function () {
bws.subscribeTrades('BTCUSD');
bws.subscribeOrderBook('BTCUSD');
bws.subscribeTicker('LTCBTC');
});

bws.on('trade', function (pair, trade) {
console.log('Trade:', trade);
});

bws.on('orderbook', function (pair, book) {
console.log('Order book:', book);
});

bws.on('ticker', function (pair, ticker) {
console.log('Ticker:', ticker);
});

bws.on('subscribed', function (data) {
console.log('New subscription', data);
});

bws.on('error', console.error);
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
var rest = require('./rest.js'),
ws = require('./ws.js');
"use strict";

var BFX = function(api_key, api_secret){
var APIRest = require('./rest.js');
var WS = require('./ws.js');

var BFX = function (api_key, api_secret){
this.api_key = api_key;
this.api_secret = api_secret;
this.rest = new rest(this.api_key, this.api_secret);
this.ws = new ws(this.api_key, this.api_secret);
this.rest = new APIRest(this.api_key, this.api_secret);
this.ws = new WS(this.api_key, this.api_secret);
};

module.exports = BFX;
BFX.WS = WS;
BFX.APIRest = APIRest;

module.exports = BFX;
45 changes: 0 additions & 45 deletions npm-debug.log

This file was deleted.