betfair / stream-api-sample-code

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

Stop from receiving the data #8

Open ajay92x opened 5 years ago

ajay92x commented 5 years ago

I'm using the following code multiple times to get the odds of different event.

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"); });

function getMarketData(marketIdArray, eventTypeIdArray, eventIdArray) { / Send authentication message / client.write('{"op": "authentication", "appKey": "APIKEY", "session": "TOKEN"}\r\n');

/ Subscribe to order/market stream / stream.write({"op":"marketSubscription","marketFilter":{"marketIds":[${marketIdArray}],"bettingTypes":["ODDS"],"eventTypeIds":[${eventTypeIdArray}],"eventIds":[${eventIdArray}]},"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); }); }

router.get('/getMarketData', function(req, res, next) {

getMarketData(req.marketIdArray, req.eventTypeIdArray, req.eventIdArray);

})

The problem is that once I start getting the odds then it continue even if I close my browser. Lets suppose in one browser tab I have open odds for cricket and in 2nd browser tab for tennis. Now, I'm getting the odds for both which is good. Now, if I close the first browser tab of cricket then I want to stop receiving the odds but in my server I can still see the odds receiving.

So, how can I stop receiving the odds once the browser is closed or is there any event to stop receving the odds.

Any help or suggestions or ideas?

liampauling commented 5 years ago

Not quite sure how you are running this and why a browser is involved but it’s a socket so you need to close it.

Client.Stop()