B4J Question [BANano] [SOLVED] How can I implement webhooks (for dummies)?

alwaysbusy

Expert
Licensed User
Longtime User
If you want your WebApp to direcly get updates from a server when something happens, you are probably better of with WebSockets.

A Webhook is just one URL registering a callback to another URL. Typical setup in B4X would be having an API on your B4J server where another server can register its callback URL. When e.g. a record is updated, the B4J server will POST (a json for example) to this callback URL with the changes of the record.

When the WebApp comes online it will get the newest information from its 'callback' server. The point is, with WebHooks, both ends have to be on-line to make a communication and with a WebApp in a browser, this isn't guaranteed and it can not be reached by an external server. WebSockets can do this because they keep the connection open between a server and a browser.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
For our API this is the workflow we use in short for a Webhook (with a jServer):

API client subscribes to one of our webhooks
Url: /v1/webhook
Body for POST:
B4X:
{
   "type": "registerRecordManipulation"
   "callBackURL": "https://theclientAPI.com/incommingWebhook"
}
-> Insert in a DB the info that this user is subscribed to this webhook

Same to unsubscribe or updating the clients callbackURL. In the header of the call, his "secret_key" determines who he is and if he is allowed to use our API.

Now in action:
Someone updates a record -> We add a record with the update info in a Queue in the database (if the user is subscribed).

the jServer has a BackgroundWorker that checks this queue every so much seconds. It then sends the message to the callbackURL of the client using a resumable sub and OkHttpUtils2.

We have a system with retries in case his callback server is offline (we also send a mail to him saying it is offline so their IT people can take action).

With B4X, this is very easy to implement because of all the build-in tools we have at our disposal with a jServer.

Alwaysbusy
 
Upvote 0
Top