-
Notifications
You must be signed in to change notification settings - Fork 28
WebSocket v2 (beta)
WebSocket feed provides real-time market data covering orderbook updates, order life cycle and trades.
In order to start receiving real time messages, a connection needs to be made followed by a message to subscribe
to channels and also marketIds you are interested.
The base url for WebSocket v2 is the same as https://socket.btcmarkets.net
tick
, trade
, orderbook
, orderChange
, fundChange
, heartbeat
all active markets as per below: https://api.btcm.ngin.io/v2/market/active
marketId format: BTC-AUD
, XRP-BTC
, etc.
Below sample code connects to tick
channel.
var socket = require('socket.io-client')(`https://socket.btcmarkets.net`, {
secure: true,
transports: ['websocket'],
upgrade: false,
path: '/v2'
});
var request = {
marketIds:['BTC-AUD'],
channels: ['tick', 'heartbeat']
}
socket.on('connect', function(){
socket.emit('subscribe', request);
});
socket.on('error', function(err){
console.log(err);
});
socket.on('message', function(data){
console.log(data);
});
{ marketId: 'BTC-AUD',
timestamp: '2019-04-08T18:56:17.405Z',
bestBid: '7309.12',
bestAsk: '7326.88',
lastPrice: '7316.81',
volume24h: '299.12936654',
messageType: 'tick'
}
Note: messageType
is available in all messages and indicates what type of data to expect from the message.
In order to receive trade
events please add trade
to the list of channels when subscribing.
{ marketId: 'BTC-AUD',
timestamp: '2019-04-08T20:54:27.632Z',
tradeId: 3153171493,
price: '7370.11',
volume: '0.10901605',
messageType: 'trade'
}
In order to receive orderbook
events please add orderbook
to the list of channels when subscribing.
Receiving events about life cycle of your orders require sending authentication information when subscribing to events. The authentication is similar to public API authentication using your API key and secret.
Below is sample node.js code with authentication:
const key = 'your api key';
const secret = 'your api key secret';
var socket = require('socket.io-client')('https://socket.btcmarkets.net', {
secure: true,
transports: ['websocket'],
upgrade: false,
path: '/v2'
});
const now = Date.now();
const strToSign = "/users/self/subscribe" + "\n" + now;
const signature = signMessage(secret, strToSign);
var request = {
marketIds:['BTC-AUD'],
channels: ['orderChange', 'heartbeat'],
key: key,
signature: signature,
timestamp: timestamp
}
socket.on('connect', function(){
socket.emit('subscribe', request);
});
socket.on('message', function(data){
console.log(data);
});
function signMessage(secret, message) {
var key = Buffer(secret, 'base64');
var hmac = crypto.createHmac('sha512', key);
var signature = hmac.update(message).digest('base64');
return signature;
}
Order change event:
{ orderId: 79003,
type: 'Limit',
openVolume: '1',
status: 'Placed',
triggerStatus: '',
timestamp: '2019-04-08T20:41:19.339Z',
messageType: 'orderChange'
}
{ orderId: 79033,
type: 'Limit',
openVolume: '0',
status: 'Fully Matched',
triggerStatus: '',
timestamp: '2019-04-08T20:50:39.658Z',
messageType: 'orderChange'
}
and another message type when order is cancelled:
{ orderId: 79003,
type: 'Limit',
openVolume: '1',
status: 'Cancelled',
triggerStatus: '',
timestamp: '2019-04-08T20:41:41.857Z',
messageType: 'orderChange'
}
Introduction updated 9/28/18
WebSocket v1 deprecated
Market Data API updated 7/24/19
Trading API updated 08/19/19
Account API updated 3/14/19
Fund Transfer API updated 08/06/19