I am trying to collect market updates from MtGox via their websocket interface. My script follows the example in
https://github.com/dlanod/node-mtgox-websocket-client with some simplifications, but it fails after working for a few hours.
The error message is usually 'Error: Stream closed unexpectedly.' coming from socket.io, but sometimes the script simply stops receiving the data without giving any error. The latter is much worse than the former, because without an error diagnostics I cannot even reconnect to the websocket.
Has anyone experienced anything similar? If you did, how did you deal with that? Any information will be highly appreciated.
Cheers,
Cyril
___
Just in case, here is the code:
var io = require('socket.io-client');
var MTGOX_SOCKET_URL = 'https://socketio.mtgox.com/mtgox';
var socket = io.connect(MTGOX_SOCKET_URL);
process.on('exit', function() {
console.log('Exiting process.');
});
socket.on('message', function(message) {
if(message.op == 'private' && message.private == 'depth')
renderDepthMessage(message);
if(message.op == 'private' && message.private == 'trade')
renderTradeMessage(message);
});
socket.on('error', function(message) {
console.log(message);
});
socket.on('disconnect', function() {
console.log('Disconnected');
});
var renderTradeMessage = function(message, lastPrice) {
console.log(getTradeFormat(message.trade, lastPrice));
};
var renderDepthMessage = function(message) {
console.log(getDepthFormat(message.depth));
};
var getDepthFormat = function(depth) {
return depth.now + ' ' + depth.type + ' ' + depth.total_volume_int + ' ' + depth.price_int;
};
var getTradeFormat = function(trade) {
return trade.tid + ' ' + trade.trade_type + ' ' + trade.amount_int + ' ' + trade.price_int;
}