Can I trigger Client-side Javascript from Server-side code?

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

chayward
Ebase User
Posts: 22
Joined: Fri Oct 13, 2017 9:43 am
Location: Hertford, UK
Contact:

Can I trigger Client-side Javascript from Server-side code?

#1

Postby chayward » Fri Mar 18, 2022 12:10 pm

Hi,

Is there any way that from the server-side, I can trigger a Clientside event?
In this app, we use SweetAlert2 to show 'popup' messages to the user. I'd like to trigger that clientside code, but it needs to be done by the server code, not client code.
HTML Element Properties -> Custom Event handlers are triggered by the user doing something in the client browser.
I need something that can be triggered by just serverside code, not a browser event.
Any way this can be done?
Thanks.

Chris.
0 x

sam
Ebase User
Posts: 4
Joined: Thu Apr 12, 2018 8:44 am

Re: Can I trigger Client-side Javascript from Server-side code?

#2

Postby sam » Fri Mar 18, 2022 12:53 pm

Hi Chris,

If you want to trigger a SweetAlert popup in response to some condition determined on the server, you'll need to use the Client-Server API.

This requires some code attached to a client-side event via HTML Element Properties -> Custom Event handlers and some server-side code as client-callable functions on the form.

You'd need to break your server-side code into two client-callable functions:

Code: Select all

function doChecks(){
  // do whatever checks you need to do
  // return information to the client code so it knows whether to show a prompt
  return {
    warning: true
  }
}

function completeAction(){
  // do whatever you need to do to complete the event
}
Obviously you can name the functions something a little more descriptive but I'm not sure what it is they're doing.

Then attach client-side code to a click event, something like this:

Code: Select all

// execute the server-side code that does the checks that might result in a warning
var result = $eb.executeFunction("doChecks", null, true, true);

// if there is a warning, show a sweet alert
if(result.warning){
  // show the alert, for example..
  Swal.fire({
    ...
  }).then((result) => {
    if (result.isConfirmed) {
      // complete the server-side action
      $eb.executeFunction("completeAction", null, true, true);
    }
    else if (result.isDenied) {
      // do nothing? maybe show another alert confirming that the action has been aborted?
    }
  })
}
else {
  // no alert required, complete the action without a prompt
  $eb.executeFunction("completeAction", null, true, true);
}
There's some material on our Resource Hub about Using the Client-Server API.
0 x

chayward
Ebase User
Posts: 22
Joined: Fri Oct 13, 2017 9:43 am
Location: Hertford, UK
Contact:

Re: Can I trigger Client-side Javascript from Server-side code?

#3

Postby chayward » Fri Mar 18, 2022 2:28 pm

Hi Sam
Thanks for that advice and code examples.
I'm using client-callable functions that give feedback response elsewhere, so I can base it on that code.
It's just this particular sceanrio is a little bit complex, but I'll use this principle, and give it a go.
Thanks for your help.

Regards,
Chris.
0 x

jezjones
Ebase User
Posts: 6
Joined: Fri Feb 10, 2017 10:17 pm

Re: Can I trigger Client-side Javascript from Server-side code?

#4

Postby jezjones » Mon Mar 21, 2022 11:42 am

I've done something like this before and used JavaScript to trigger that API call every x mins.

Polling like this may be ok, but I think this is where webSockets come in. It would be good if Verj had this out of the box.
0 x


Who is online

Users browsing this forum: No registered users and 60 guests