javascriptlightstreamer

Add event handler to LightStreamer


I want to scrape a website which uses LightStreamer for showing live data. How can i add an event handler to receive those messages in Javascript?
Unfortunately, LightStreamer doesn't have enough tutorial.
Following sample works for me:

var myEH = function ff(msg) {
            console.log("message receieved.");
        };
        
App.ls.getSubscriptions()[0].addListener({ onItemUpdate : myEH})

When i repalce "message receieved." with msg variable, i see a non-sense array which is not similar to what i see in Chrome DevTools (Network tab> ws section).

Also, there are isActive() and isSubscribed() method for each subscription. When i check them against array elements returned by getSubscriptions(), i see that all of them are true. But, in fact, Chrome DevTools shows that only one them that is active and receives message. Finding active subscription is another problem.

Update: Thanks @Robert for his help. Finally, i could understand that getSubscriptions()[6] includes what i need. Other subscriptions update clock or other things in page. I extracted items and fields from this subscription as following:

var myItems = App.ls.getSubscriptions()[6].getItems();
var myFields = App.ls.getSubscriptions()[6].getFields();
var myEH3 = function ff(msg) {
            console.log('tttt3'); 
        };
        
var mySub = new Subscription("MERGE", myItems, myFields);
mySub.addListener(myEH3);
App.ls.subscribe(mySub);

But this doesn't work. Server returns error 'Data Adapter Not found.' Changing to 'mySub.addListener({ onItemUpdate: myEH3});' does not help. I have already tried to add eventhandler directly to getSubscriptions()[6] via 'App.ls.getSubscriptions()[6].addListener({ onItemUpdate : myEH3});', but my function never called. Any Hint would be greatly apprecieated.


Solution

  • Finally, I could get my head around this library. Searching JavaScript files of webpage, I found that somewhere DataAdapter will be set to subscriptions.
    This is my final code:

    var myItems = App.ls.getSubscriptions()[6].getItems();
    var myFields = App.ls.getSubscriptions()[6].getFields();
    var adaptername = App.ls.getSubscriptions()[6].getDataAdapter();
    var myEH3 = function ff(msg) {
                console.log('message received.'); 
            };
            
    var mySub = new Subscription("MERGE", myItems, myFields);
    mySub.setDataAdapter(adaptername);
    mySub.setRequestedSnapshot("yes");
    
    mySub.addListener({onItemUpdate: myEH3});
    App.ls.subscribe(mySub);