betfair / stream-api-sample-code

Sample code for the exchange stream api which provide real time market and order data from betfair.
73 stars 60 forks source link

Socket has been ended by other party #7

Open ajay92x opened 4 years ago

ajay92x commented 4 years ago

I have tried using the following code to get the market data and it is working but sometimes I'm getting the error "socket has been ended by other party" and then I have to restart the server to make it work again.

var tls = require('tls');

/ Socket connection options /

var options = { host: 'stream-api.betfair.com', // host: 'stream-api-integration.betfair.com', port: 443 }

/ Establish connection to the socket /

var client = tls.connect(options, function () { console.log("Connected"); });

/ Send authentication message /

client.write('{"op": "authentication", "appKey": "APIKEY", "session": "TOKEN"}\r\n');

/ Subscribe to order/market stream / stream.write({"op":"marketSubscription","marketFilter":{"marketIds":["1.160865586"],"bettingTypes":["ODDS"],"eventTypeIds":["2"],"eventIds":["29390842"]},"marketDataFilter":{}}\r\n);

client.on('data', function(data) { console.log('Received: ' + data); });

client.on('close', function() { console.log('Connection closed'); });

client.on('error', function(err) { console.log('Error:' + err); })

liampauling commented 4 years ago

Yeah this happens, it’s do with the socket connection being closed / dropped.

Not sure on the details of this library but you need to catch the error and resubscribe, nothing else you as a client can do.

ajay92x commented 4 years ago

@liampauling Thanks for your response. I'm resubscribing right now on connection drop. I thought there must be some other way to prevent this error from originating.