Xojo and javascript back and forth

Hi everyone. I’am new to Xojo, I come from php and Javascript.
I’m creating a custom control where xojo passes javascript some commands to execute (they add rows to a table) but for some of these commands/rows js needs more info, so it triggers back the triggerserverevent. Xojo receives it and executes code before subsequent commands/rows (for example it must process rows yet to be added).
Has anyone faced this situation before?
I’ve tried to create a buffer of commands in xojo, so that Serialize defer sending some commands, but I can’t. Something goes wrong.
Is it the right way? Any idea?
Thanks.
Guido

Just so you understand what’s going on under the hood:

Every time an “event” is received from the browser ( an http request) the Session for that browser is brought to the foreground and the server executes that event. The executable follows every bit of code that occurs as a result of that command and queues up all of the changes that are made into a single response. Once all of the user code has executed, a single json item is sent back to the browser, containing all of the changes that were made, including calls to execute JavaScript.

Everything that you do must be done within the context of that users session. If not, you run the risk of either sending it to the wrong session or having it run on the main thread, which could tie up resources with no result.

If you have something that must be run in the background for a particular session, you should be using a WebThread which originates from within the session context that you want it connected to. That way, when its code runs, the responses will be sent to the correct browser. Make sure you call self.sleep inside the threads code periodically to allow other things to run or your app will lock up.

1 Like

Hi Greg,
Can you post an example of code for self.sleep in a thread?
thank you.

Uh…

Self.Sleep

Basically, anywhere that you would be running a tight loop that could consume or block the CPU. you just need to give time to the rest of the app so it doesn’t lock up waiting for your code to finish.

ONLY within the Run event (or a method called from it) of a WebThread.

if you mean to say that it is good to insert Self.Sleep during the execution of the actions contained in the WebThread’s Run event, what could be approximately the time interval between one Self.Sleep and the next?

It depends on how many sessions are running. Remember, Xojo uses cooperative threading so you need to yield adequate time.

The Sleep method takes a parameter, in milliseconds of the amount of time you want the thread to sleep, which defaults to 10, but it’s a request and the actual time may vary.

Welcome @Guido_Canella!

Isn’t the TriggerServerEvent / UpdateControl combination enough in this case? Check the “Bootstrap Toast” Web SDK example, it uses updateControl to receive commands from the server.

I’m not sure how the control you’re creating works, but there is also a WebSDKUIControl event, called HandleRequest, that might help in this case.

From the WebSDK documentation PDF:

You’ll have to do some plumbing yourself (making the request in JavaScript, sending back the correct MIME type, …), but the response will include just what you need.

2 Likes

Thanks to everybody, I fixed the problem.
The problem was that i didn’t implement the event in the instance of the class, so ExecuteEvent didn’t handle it.
I admit that I haven’t studied very much, but I can’t figure out how to do it in case a defined event isn’t implemented in the subclass: if it isn’t implemented, ExecuteEvent doesn’t handle the event at all, while I need ExecuteEvent to identify and manage it anyway. I tried with Return True in ExecuteEvent, but it doesn’t work.
Maybe it’s something basic, any suggestion is welcome!

When you call triggerServerEvent, the final parameter is a boolean. If you provide true, the event will be sent regardless of whether the Xojo web framework thinks that event has been implemented or not.

From the WebSK Docs PDF:

triggerServerEvent(eventName: string, parameters: JSONItem | null = null, forced: boolean = false)

Sends an event to the server for processing. eventName should match the Xojo event name. parameters are of type JSONItem. The forced parameter should be used sparingly, it is meant for sending events that otherwise send important information to your control even if the event hasn’t been explicitly implemented. For example, changes to the text in a textfield are always sent because the user has a reasonable expectation that the Value property of the field will be up to date, regardless of whether he or she implemented the ValueChanged event.
triggerServerEvent(eventName: string, parameters: JSONItem | null = null, forced: boolean = false)
Sends an event to the server for processing. eventName should match the Xojo event name. parameters are of type JSONItem. The forced parameter should be used sparingly, it is meant for sending events that otherwise send important information to your control even if the event hasn’t been explicitly implemented. For example, changes to the text in a textfield are always sent because the user has a reasonable expectation that the Value property of the field will be up to date, regardless of whether he or she implemented the ValueChanged event.

1 Like