HandleSpecialURL - necessity for app to have session running ?

I have been working with HandleSpecialURL to allow a third party website to communicate with my web based app.

The third party site sends a http get to my handleSpecialURL handler, and this works. My web based app sends me off an email to indicate it received the comms, and writes to the local log file in the Xojo Cloud Documents Folder.

But this only works when there is an active session running on my web based app. If there is no session running, I don’t get an email.

My web based app has code in the HandleSpecialURL event which resides in the App domain (or environment). When the event is invoked, I write to a local text file on the server (in Xojo Cloud). I also send email. All these functions are in the App domain, not in the Session or a Web Page.

Is this expected behaviour ? If so, what is the secret sauce to make my web based app wake up when HandleSpecialURL is invoked from web-side and do what it is supposed to do ? If not, what should I do to make it all Just Work ?

Also, is there a way to get a desktop app to establish a session with a web based app (living in Xojo Cloud) ? Should this be done via a TCP/IP socket or a HTMLviewer ?

Regards,
Tony Barry

[quote=127397:@Tony Barry]I have been working with HandleSpecialURL to allow a third party website to communicate with my web based app.

The third party site sends a http get to my handleSpecialURL handler, and this works. My web based app sends me off an email to indicate it received the comms, and writes to the local log file in the Xojo Cloud Documents Folder.

But this only works when there is an active session running on my web based app. If there is no session running, I don’t get an email.
[/quote]
Do you have the app running as CGI ? Or more to the point, is the App actually running. When run as a CGI the app would shutdown after the last session closes.

[quote=127397:@Tony Barry]The third party site sends a http get to my handleSpecialURL handler, and this works. My web based app sends me off an email to indicate it received the comms, and writes to the local log file in the Xojo Cloud Documents Folder.

But this only works when there is an active session running on my web based app. If there is no session running, I don’t get an email.[/quote]

What about the log ?

A cgi app would launch automatically when its URL is called, so specialurl or not, it should restart automatically.

Tony,

Even though you are not getting the email, is the connection being written to the log?

Just a hunch : where is the method that sends email ? If it is anywhere in Session or on a page, of course it won’t work for handlespecialurl since that event is in App. Only a method in App or in a module can be called when there is no session.

He said it’s in the app class, but I could see that the app doesn’t stay running long enough to send the email with no sessions running.

I have a couple of web apps that use HandleSpecialURL and never have a Session open. I think we need more information from Tony.

You won’t either. Only a browser will start a session.

SMTPSocket/SMTPSecureSocket are asynchronous. It probably initiates the email transfer then falls out of scope before it finishes. Maybe make the SMTP socket a property of App object. However then you can only use one. Ideally you write the email to a log, then another background job comes along and sends the appropriate outgoing emails.

Another approach is use something like Mailgun and use their REST API to send emails. Then you can use a synchronous HTTPSocket.

The conclusion comes naturally : one socket dedicated to the task of sending the above mentioned email in app, or even in the handlespecialurl event, to make sure it does not get out of scope.

Hi All,
Thank you to all who replied. The input is muchly appreciated.

Yes the web app is CGI.
All the code for receiving the third party input is within the app class.
All the email code is within the app class.
All the logfile write code is within the app class.
The code for opening the firewall is within the app class.
The SMTPsecureSocket is a property of the app class.
The log file is also written to only when a session is active.

I went through the code fairly carefully after reading the above messages, and discovered that I did not instantiate the SMTPsecureSocket within the handleSpecialRequest event. This is likely the cause of the fail.

Normally, I would expect some kind of error (nilObject) in some relevant place. Unfortunately I do not know where a Xojo Cloud app will put such errors.

Regards,
Tony Barry

One further thing about the handleSpecialURL code I used.

I called the email send code first, then the write to the log file.

Since the email socket was not instantiated, the handleSpecialURL event failed. This meant the log file never got written to either.

Regards,
Tony Barry

Not a failure, a crash. Basically, you had a NilObjectException which stopped execution immediately.

But regardless. SMTPSecureSocket runs asynchronously. Unless you add some code to hold the app running, it’ll still quit before the email goes out.

Hi Greg,

Many thanks for the tip.

How would I “add code to hold the app running” ? What form would this take ?

Regards,
Tony Barry

Your best bet is probably to subclass SMTPSecureSocket and do something like this:

Add a property, MailDelivered as Boolean.

In the MailSent event, set MailDelivered = True

In your code, after you call SendMail,

While Not MailDelivered App.DoEvents(100) Wend

Then use the subclass instead.

OK.

I’ll see how this goes.

This does mean that handleSpecialURL could be called recursively if I have a slow send on the email. Is that a problem ? My HTTP GETs to the app are likely to come in every couple of seconds under the right circumstances.

Regards,
Tony Barry

It would probably be okay, but if you’re really concerned, move the email code to a separate console app and just call it using a shell. Then you don’t need to worry about whether the app stays running.

Keep in mind though… If you’ll be getting hit every couple seconds, it might be worth setting App.AutoQuit = False in the App.Open event so the app will stay running. Then maybe once per day you could set it to True so the app will quit once in a while.

Thank you Greg.

App.AutoQuit is already se to false, but I can work out a schema to restart the app every day.

This “handleSpecialURL” is a great thing. It is a really powerful tool to make cloud based transactions accessible to the common man (or woman).

Regards,
Tony Barry

I’m saying "set it to True in the App.Open event and then periodically set it to False so the app can quit on it’s own once per day.

Ummm, I think you had a typo there?

I think you’re saying “set it (App.AutoQuit) to False in the App.Open event and then periodically set it to True so the app can quit on it’s own once per day.”