kmm2908
Hi trying to use LS with IG api and having trouble getting a subscription set up.
Keep getting following error:
Error encountered !
Status code: unknown
Error code: ReferenceError: subscription is not defined
Can get data from API call no problem.
Can connect to LS no problem.
But setting up the subscription is proving very problematic.
I've added the function code below if anyone can help much appreciated:
// Now subscribe to the BID and OFFER prices
var fieldList = ["bid","offer","updateTime","delayTime","marketStatus"];
require(["Subscription"], function (Subscription) {
// Set up Lightstreamer FIDs
var subscription = new Subscription("MERGE","L1:IX.D.DAX.IFD.IP",fieldList)
});
// Set up Lightstreamer event listener
subscription.addListener({
onSubscription: function () {
console.log('subscribed');
},
onSubscriptionError: function (code, message) {
console.log('subscription failure: ' + code + " message: " + message);
},
onItemUpdate: function (updateInfo) {
// Lightstreamer published some data
// Add a new row to data table
var marketStatus=getValue(marketStatus);
var bidprc=getValue(bid);
var offprc=getValue(offer);
var delayTm=getValue(delayTime);
var updTm=getValue(updateTime);
var chg = "";
$('#search_results_list').find('tbody:last').append(''+marketStatus+''+updTm+''+delayTm+''+bidprc+''+offprc+''+chg+'');
}
});
// Subscribe to Lightstreamer
lsClient.subscribe(subscription);
Giuseppe Corti
Hi kmm2908,
It should be a problem about the scope of subscription variable; in your code it is visible only within the require block. Please try something like this:
[SYNTAX=JSCRIPT]
// Now subscribe to the BID and OFFER prices
var fieldList = ["bid","offer","updateTime","delayTime","marketStat us"];
var subscription;
require(["Subscription"], function (Subscription) {
// Set up Lightstreamer FIDs
subscription = new Subscription("MERGE","L1:IX.D.DAX.IFD.IP",fieldLis t)
// Set up Lightstreamer event listener
subscription.addListener({
onSubscription: function () {
console.log('subscribed');
},
onSubscriptionError: function (code, message) {
console.log('subscription failure: ' + code + " message: " + message);
},
onItemUpdate: function (updateInfo) {
// Lightstreamer published some data
// Add a new row to data table
var marketStatus=getValue(marketStatus);
var bidprc=getValue(bid);
var offprc=getValue(offer);
var delayTm=getValue(delayTime);
var updTm=getValue(updateTime);
var chg = "";
$('#search_results_list').find('tbody:last').appen d(''+marketStatus+''+updTm+''+delayTm+''+bidprc+'' +offprc+''+chg+'');
}
});
// Subscribe to Lightstreamer
lsClient.subscribe(subscription);
});[/SYNTAX]