0

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.

ALalavi
  • 115
  • 1
  • 11
  • what you mean non sens array. you get [ItemUpdate](https://sdk.lightstreamer.com/ls-web-client/8.0.2/api/ItemUpdate.html). on this instance you have serveral method like forEachChangedField and getItem name. you have to figure out witch one to use to get your data. – Robert Nov 14 '20 at 12:54

2 Answers2

2

They have tutorial, and solid api documentation... from

<script>
  require(["LightstreamerClient", "Subscription", "StaticGrid"], function(LightstreamerClient, Subscription, StaticGrid) {
    var client = new LightstreamerClient(null, "HELLOWORLD");
    client.connect();
    // you have to create your own SubscriptionListener
    var grid = new StaticGrid("hellogrid", true);
    
    var subscription = new Subscription("MERGE", grid.extractItemList(), grid.extractFieldList());
    subscription.addListener(grid);
    
    client.subscribe(subscription);
  });      
</script>  

Now you have to create your own subscription listener according to follow interface. And then you have onItemUpdate on your subscription listener.

Robert
  • 2,538
  • 1
  • 9
  • 17
  • Thanks @Robert. I've already seen those links, but i couldn't find my way through them. In webpage, there is an already created LightstreamerClient which gets data from server and i can confirm them by looking at ws tab in Chrome DevTools. I was thinking about a way to attach one more event handler to this LightstreamerClient to get messages. I can't create new LightstreamerClient because i don't know which items/fields should be populated to receive exactly those messages. – ALalavi Nov 14 '20 at 11:34
  • i think that you have to add new subscription listener, and listne on them onItemUpdate event. btw you should show your code sample, for that you have -1 rank for this question. – Robert Nov 14 '20 at 11:47
0

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);
Dharman
  • 30,962
  • 25
  • 85
  • 135
ALalavi
  • 115
  • 1
  • 11
  • If you have a new question then search existing questions or ask a new one, but don't ask follow up questions in your answers – Dharman Nov 15 '20 at 14:58