NetTalk Central

Author Topic: Ajax XMLHttpRequest and a kind of push notifications  (Read 6133 times)

Alberto

  • Hero Member
  • *****
  • Posts: 1873
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Ajax XMLHttpRequest and a kind of push notifications
« on: August 28, 2020, 04:12:40 AM »
Hi, XMLHttpRequest has no timeout, then I could use it to send a GET to a NT page and that page to wait until an event and then response to the GET.
The XMLHttpRequest GET would be as a : "Hey Ntpage... Im here, waiting for news" and when the news appears the NT page produce the responce.
How is the best way to wait for the news in a Ntpage before response?
Is this possible?
Thanks
-----------
Regards
Alberto

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #1 on: August 30, 2020, 10:49:17 PM »
It sounds like you are trying to re-invent web sockets.
Why not, um, just use web sockets for this?

cheers
Bruce

Alberto

  • Hero Member
  • *****
  • Posts: 1873
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #2 on: August 31, 2020, 05:09:04 AM »
And how to do it using web sockets?
I have an ajax function that query the server about changes in a table and updates the values in a browse cell.
-----------
Regards
Alberto

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #3 on: August 31, 2020, 10:33:55 PM »
Basic Web Sockets are discussed here;
https://www.capesoft.com/docs/NetTalk11/NetTalkWebBasic.htm#WebSockets

The NetRefresh notes in there though would apply to the whole browse, not just one cell.
But your JavaScript code can use the functions in nt-websockets.js to "watch a variable, and then call your own callback function when something changes. For example;

nts.watch("watchid","","sessionvalue","someotherid",somefunction,someparameter);

watchid is some arbitrary (unique) Watch ID - this can be anything, you make it up, it's used to separate items in the "watch queue".
This ID allows you to update or change watches etc.

The second parameter is the URL of the server. Leave this blank to use the same server as the current page.

the 3rd parameter is the scope. Use "sessionvalue" or "hostvalue" (or "channel") depending on the type of thing on the server you are wanting to watch.

The 4th parameter is the id of the HTML element on the page. If this is set, and it is an input field, then the value in the field will be automatically updated when the "watched" value changes. If it is set, but is not an input field, then the HTML contents of the element will be set to the new value. This parameter ONLY applies if the 5th parameter is omitted or undefined. If a callback function is passed in (5th parameter) then this parameter is effectively ignored.

the 5th parameter is the Callback function. If this is specified then the 4th parameter is ignored. Instead, when the value changes the callback function will be called. The function will receive 2 parameters, the first is a JSON structure containing the details of the change, the second is the someparameter value above.

nts is a global - page level - Json object that is automatically instantiated for you.

cheers
Bruce

Alberto

  • Hero Member
  • *****
  • Posts: 1873
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #4 on: September 01, 2020, 05:05:35 AM »
Thank you very much for the explanation!
Then...
I have a browse with many rows and some columns and need to update it as fast as possible.
Suppous I have 1000 html IDs to update.
I suppouse, I also need one session variable for each cell that I need to change in the browse.
Is it possible to code 1000 nts.watch() to update each one?
Does this makes sence?

Or may be just have one nts.watch to call a function when a session variable changes in the server side and use this function to, using AJAX, retreive the changes and update the html IDs. Something like:
- When the server makes the browse, save the rows id in a session variable to know what the client is watching in the browse, lets call it "myRowsToWatch" session variable.
- Then call nts.watch("xxx","","myYouNeedToUpdate","",MyAjaxFunction,)
- In the server when some value in any column of any row in the myRowsToWatch session variable changes, then set the myYouNeedToUpdate+=1
- This should then call the MyAjaxFunction which makes a GET to a server page indicating it needs to receive the changes, it receives the changes and updates the browse cells.

Is this a logic approach?

PD: In the "update the browse cells" proc Im having a problen, I need to calculate the same ID NT is using for each cell and js CRC32 is not generating the same as clarion, any way of change the way NT names the ids? something like "div"+row+column+"_div" ?
-----------
Regards
Alberto

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #5 on: September 01, 2020, 10:21:23 PM »
Hi Alberto,

If you are updating 1000 HTML Id's then it might be a lot more efficient to simply refresh the whole browse.
You can "watch" a single session, or host, variable and then when that happens trigger a browse refresh in your JavaScript function.

>>I suppose, I also need one session variable for each cell that I need to change in the browse.
>> Is it possible to code 1000 nts.watch() to update each one?

It is, as long as they all have a unique first parameter. That said while it's possible, I don't know how fast it will be. That's quite a lot of extra work on both the server and client side. You would need to test ti to see what performance is like.

cheers
Bruce


Alberto

  • Hero Member
  • *****
  • Posts: 1873
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #6 on: September 02, 2020, 03:33:49 AM »
Ok, thanks and...


PD: In the "update the browse cells" proc Im having a problen, I need to calculate the same ID NT is using for each cell and js CRC32 is not generating the same as clarion, any way of change the way NT names the ids? something like "div"+row+column+"_div" ?

Regards
-----------
Regards
Alberto

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #7 on: September 06, 2020, 04:07:38 AM »
I'm not sure I understand the question....

>> I need to calculate the same ID NT is using for each cell

calculate it from _what_?

>> and js CRC32 is not generating the same as clarion

but in JS you _have_ the ID, no need to calculate it...

I'm clearly not understanding...

B

Alberto

  • Hero Member
  • *****
  • Posts: 1873
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #8 on: September 06, 2020, 04:11:31 AM »
To change the value of a htnl element, in this cas a cell of a browse I need the ID
Yo set the IDs using 'div'+CRC32('text indicatine name of the proc/browse and ID of the row record')+'_div'
You do this in clarion code.
In JS, knowing the 'text indicatine name of the proc/browse and ID of the row record' I need to calculate the ID in order to change it.
CRC32 in JS give different results that in clarion then I cant calculate the ID
-----------
Regards
Alberto

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #9 on: September 06, 2020, 10:44:09 PM »
>> In JS, knowing the 'text indicatine name of the proc/browse and ID of the row record' I need to calculate the ID in order to change it.

where would the JS get all this information though? The ID of the record is not in the HTML.

I think you are asking the wrong questions again. I think you are trying to solve your root problem the wrong way, and this is causing you to go down this route, and hence you're getting stuck on the wrong problem.

Bruce

Alberto

  • Hero Member
  • *****
  • Posts: 1873
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #10 on: September 07, 2020, 05:03:15 AM »
I think Im not explainig very well, sory for that, again:

I have a js function with a parameter.
In this parameter It receives the list of records Ids Im listing in the browse.
I call it like
MyFunction('1,11,22,33,...')
1,11,22,33 are the browse records ids, Im talking about the PRE:ID field of the browse Sql file Im listing.

This function uses Ajax to query the server if some of this records have changed.

The server response with an xml with the record id (PRE:ID field) and the new value (PRE:VALUE).

To change the html value of the record in the web page, I need to know its "html id", then I need to calculate it.

Hope this is more clear.

Regards


-----------
Regards
Alberto

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #11 on: September 07, 2020, 10:01:52 PM »
>> To change the html value of the record in the web page, I need to know its "html id", then I need to calculate it.

why not just pass these to your JavaScript function to begin with?
You know how to calculate them on the clarion side...

cheers
Bruce

Alberto

  • Hero Member
  • *****
  • Posts: 1873
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: Ajax XMLHttpRequest and a kind of push notifications
« Reply #12 on: September 08, 2020, 03:15:22 AM »
Im doing this but it would be better not to do it.
If you have 500 and you have an id of 14 chars you got a parameter of 7000 and I need to keep it in an array to retreive it when the server response arrives, and so on...
-----------
Regards
Alberto