Notifications
How to send and receive real-time messages
In Dart, the AtClient is stored within the AtClientManager. Once an atSign has been onboarded, you will be able to access the AtClientManager for its associated atSign.
AtClientManager
AtClientManager is a singleton model. When AtClientManager.getInstance() is called, it will get the AtClientManager instance for the last onboarded atSign.
AtClientManager atClientManager = AtClientManager.getInstance();AtClient
As previously mentioned, the AtClientManager stores the actual AtClient itself. You can retrieve the AtClient by calling atClientManager.atClient.
AtClient atClient = atClientManager.atClient;Monitor
You can subscribe to a stream of notifications like this:
AtClient atClient = AtClientManager.getInstance().atClient;
atClient.notificationService
.subscribe(regex: 'message.$nameSpace@', shouldDecrypt: true)
.listen((notification) {
print("Got a message: ${notification.value}");
});Notify
You can send (a.k.a. publish) a notification like this:
C
You can find the full code of this example on our GitHub.
Table of Contents
Introduction
Events is how we send and receive real-time messages in the atProtocol. The client SDK assists in using the atProtocol simply and handles all the complex encryption stuff for you.
notifyis how we send a real-time message to an atSignmonitoris how we listen for real-time messages
Monitor
1. Include `monitor.h`
2. Create a Monitor Context
First, we need to create a separate monitor connection to our atServer. This is similar to creating a atclient context for conducting CRUD operations, but it is important to make a completely separate one so that connection can be clear of any noise and used purely for monitoring for notifications.
3. Call `atclient_monitor_pkam_authenticate`
This is similar to the atclient_pkam_authenticate that you know and love. It should use the same atserver_host, atserver_port, and atclient_atkeys in your original atclient context because we are connecting to the same atServer that can be authenticated using the same set of cryptographic atKeys.
This function will return an int for error handling in which a non-zero exit code indicates an error.
4. Call `atclient_monitor_start`
This will begin the monitoring process of the atclient context that you pass. Since we made a completely different connection, this context will be purely for listening for any notifications.
In this example, we pass .* for the regex to receive all incoming notifications.
This function will return an int for error handling in which a non-zero exit code indicates an error.
5. Call `atclient_monitor_read`
Calling atclient_monitor_read will attempt to read the network buffer for any notifications.
It may be useful to put this in a loop to constantly be checking for any notifications that may have arrived to your atServer. You will commonly get error code -26624 because most times, there will be nothing to read!
See the Example Application on how to read the different kinds of values from the atclient_monitor_response struct.
6. Free everything
Don't forget to free everything at the end of your application's life time
Also free the atclient_monitor_response struct if you haven't already.
Example Application
You can find the full code of this example on our GitHub.
Notify
1. Include `notify.h`
2. Create a Shared atKey
Since we are notifying another atSign, we have to set up a Shared atKey.
This process should be familiar to you if you have already gone through CRUD Operations.
3. Set up Notify Params
Next, we will set up the parameters before sending the notification. This will essentially hold the instructions on how to send the notification.
The atKey and value are the two fields that are mandatory in order to send a notification. If you forget to set these two fields, then you will not be allowed to send a notification because the instructions of sending a notification are incomplete.
4. Call `atclient_notify`
Simply call atclient_notify .This will send a notification to the shared_with atSign that you specified when creating the Shared atKey which was done in this step.
We will pass NULL into the commit_id parameter because we don't reallty care about the commt id for now. You can receive it by passing a int * if you would like.
5. Free everything
Don't forget to free everything
Example Application
You can find the full code of this example on our GitHub.
Last updated

