Modern applications are expected to react instantly to changes. Whether a new email arrives, a calendar event is updated, or an item is deleted, users expect the interface to stay synchronized without requiring manual refreshes.
To simplify this process, 4D 21 R4 introduces a unified notification system in 4D NetKit for both Google Workspace and Microsoft 365. With a single programming model, you can subscribe to mail and calendar changes and react automatically whenever data changes.
HDI Microsoft Google Notifications
One of the main advantages of 4D NetKit is that the notification APIs work exactly the same way regardless of the provider.
Whether you are using Google or Microsoft 365, the notifier APIs share the same behavior:
cs.NetKit.Google.new($myOAuth2Provider).mail.notifier($notification)
cs.NetKit.Office365.new($myOAuth2Provider).mail.notifier($notification)
The same principle applies to calendar notifications.
What Can You Monitor?
4D NetKit allows you to subscribe to changes on both emails and calendar events.
You can react to:
- Creation
- Modification
- Deletion
of:
- Emails
- Calendar events
As soon as a change is detected, your application receives a notification and can update the user interface, refresh data, display alerts, or trigger custom business logic.
Choosing a Listening Mode
Before starting a notifier, you need to define how 4D NetKit will detect changes.
Two modes are available.
Real-Time Webhook Notifications
The first option is to let Google or Microsoft call your application whenever a change occurs.
To enable this mode, provide an endPoint property containing the callback URL:
$parameter.endPoint:="https://mydomain.com/notifications"
The endpoint must:
- Be publicly accessible
- Use a valid domain name
- Be secured with a trusted SSL certificate
- Have a certificate that matches the domain name
When configured, Google or Microsoft will notify your application directly whenever a subscribed resource changes.
This approach provides the fastest possible reaction time and is ideal for web applications and cloud-hosted solutions.
// Microsoft Graph webhook handler
[
{
"class": "NetKit.GraphNotificationHandler",
"method": "getResponse",
"regexPattern": "/4dnk-graph-notification",
"verbs": "post"
}
]
// Google webhook handler
[
{
"class": "NetKit.GraphNotificationHandler",
"method": "getResponse",
"regexPattern": "/4dnk-google-notification",
"verbs": "post"
}
]
Note that Gmail requires an additional parameter: topicName, which specifies the Google Cloud Pub/Sub topic used by the Gmail API to publish mailbox change notifications.
Timer-Based Monitoring
If you do not provide an endpoint, 4D NetKit automatically switches to timer-based monitoring.
In this mode, 4D NetKit periodically queries the Google or Microsoft APIs to detect changes.
$parameter.timer:=60
In this example, 4D NetKit checks for updates every 60 seconds. This approach is particularly useful when: Your application is running on a local network A public HTTPS endpoint is not available You prefer a simpler deployment model
The timer mode is enabled automatically whenever no endpoint is provided.
Defining Callback Functions
Whether you are monitoring emails or calendar events, the callback model is identical.
A convenient approach is to create a dedicated class that centralizes all notification handling.
For example, you can create a NetKitEventNotification class:
// This function is triggered when a mail or calendar event is created
Function onCreate($provider : Object; $event : Object)
var $resource:=$event.type="mailCreated" ? "Email" : "Calendar event"
ALERT("You have a new "+$resource+" event")
// This function is triggered when a mail or calendar event is deleted
Function onDelete($provider : Object; $event : Object)
var $resource:=$event.type="mailDeleted" ? "Email" : "Calendar event"
ALERT("A "+$resource+" has been deleted")
// This function is triggered when a mail or calendar event is modified
Function onModify($provider : Object; $event : Object)
var $resource:=$event.type="mailModified" ? "Email" : "Calendar event"
ALERT("A "+$resource+" event has been modified")
In the example above, no endPoint property is declared. Notifications, therefore, run in timer-based monitoring mode, using the default 30-second interval unless you set a custom timer value. If you want to use Real-Time Webhook Notifications, defining an endPoint is required:
// Set this URL to use Real-Time Webhook Notifications
property endPoint:="https://mydomain.com/googlenotifications"
// Gmail webhook mode only: Google Cloud Pub/Sub topic used for mailbox notifications
property topicName:="projects/my-project/topics/gmail-notifications"
// This function is triggered when a mail or calendar event is created
Function onCreate($provider : Object; $event : Object)
var $resource:=$event.type="mailCreated" ? "Email" : "Calendar event"
ALERT("You have a new "+$resource+" event")
// This function is triggered when a mail or calendar event is deleted
Function onDelete($provider : Object; $event : Object)
var $resource:=$event.type="mailDeleted" ? "Email" : "Calendar event"
ALERT("A "+$resource+" has been deleted")
// This function is triggered when a mail or calendar event is modified
Function onModify($provider : Object; $event : Object)
var $resource:=$event.type="mailModified" ? "Email" : "Calendar event"
ALERT("A "+$resource+" event has been modified")
Starting a Notification Subscription
Once your callbacks are defined, creating a notifier is straightforward:
// Create a notification handler instance.
var $notification:=cs.NetKitEventNotification.new()
// Create a Gmail notifier using the OAuth2 provider and the handler.
var $gmailNotification:=cs.NetKit.Google.new($myOAuth2Provider).mail.notifier($notification)
// Start listening for Gmail notifications.
$gmailNotification.start()
// Create a Google Calendar notifier using the same handler.
var $calendarNotification:=cs.NetKit.Google.new($myOAuth2Provider).calendar.notifier($notification)
// Start listening for Calendar notifications.
$calendarNotification.start()
Conclusion
4D NetKit notifications make it easy to keep your application synchronized with Google Workspace and Microsoft 365 data. With support for both email and calendar changes, you can quickly build responsive applications that react to user activity in real time.
Best of all, the programming model remains identical across providers, allowing you to write your notification logic once and use it everywhere.
Comments are not currently available for this post.