Notification 2.0 API with a Practical Microservice Example
After exploring the Notification 2.0 API in 2023, several insights have emerged:
The Notification API is relatively new and therefore lacks an abundance of examples illustrating practical applications. Additionally, the API introduces complexities that may pose challenges for new developers seeking optimal usage. In response, PS has developed a new example rooted in real-world N2.0 use cases. This example aims to showcase best practices for effectively utilizing the API while steering clear of common pitfalls that developers may encounter.
In particular, Professional Services (PS) consultants seek a comprehensive example that allows them to seamlessly integrate code and concentrate on their business logic. The desire is to sidestep the complexities of WebSocket connection management, multitenancy, reconnects, and cleanup. To address this, we have contributed sample code to a Git repository, and the subsequent section outlines its key features. I trust that this resource proves beneficial. Don’t hesitate to reach out if you envision additional features or have suggestions for enhancements. Your feedback is greatly appreciated.
The Notification-2-Sample-MS repository offers a practical demonstration of a microservice leveraging the Notification 2.0 API. This example delves into various aspects of the API, shedding light on common scenarios encountered by developers.
Here is the link to the github repository: SoftwareAG/Notification-2-Sample: This repository contains a practical example of a microservice utilizing the Notification 2.0 API. (github.com)
- Subscription to a device in managed object context.
- Subscription in tenant context.
- Example of subscribing for measurements.
- Subscription to all devices.
- Handling disconnects and reconnect logic.
- Posting an alarm directly on the microservice if a disconnect is detected.
- Demonstrates support for deployment on the management tenant, with multiple subtenants subscribing to it.
- It’s crucial to unsubscribe a subscriber if it’s no longer needed to prevent storage issues caused by continuous notifications.
- The microservice includes logic to raise a critical alarm upon disconnect detection and clear the alarm once reconnected.
- Immediate reconnection attempts may lead to a 409 Conflict error. The example retries with a delay to avoid this issue.
Explicit Management of WebSocket Connection State
- The example manages the WebSocket connection state explicitly.
WebSocket Library Consideration
- Explore WebSocket libraries like tooTallNate for automatic reconnects.
Subscribing to All Devices
- While generally not recommended, the sample demonstrates how it can be achieved, recognizing its frequent need.
- For actual deployment, consider enhancing the code through multi-threading, utilizing multiple threads and shared resources.
This microservice assumes that it subscribes to all devices and maintains an open connection indefinitely.
These tools are provided as-is, without warranty or support, and do not constitute part of the Software AG product suite. Users are free to use, fork, and modify them, subject to the license agreement. While Software AG welcomes contributions, inclusion in the master project cannot be guaranteed. Refer to the documentation for more information.