Push Notifications: To Connect to the SignalR Hub
The following activity shows you how to connect the external application to the SignalR hub.
Story
Suppose that you want your external application to receive push notifications from MYOB Acumatica but you cannot publish a webhook (for example, for security reasons). Instead you need to configure the system to send notifications to the SignalR hub, from which any connected application can receive the notifications.
Process Overview
In a project of your MYOB Acumatica extension library, you will implement the code for sending notifications to the SignalR hub.
System Preparation
Before you begin performing the steps of this activity, do the following:
- Prepare an MYOB Acumatica instance with any predefined dataset. You can do it by completing the following prerequisite activity: Instance Deployment: To Deploy an Instance with Demo Data.
- Create a customization project, such as in the Customization Projects: To Create a Customization Project prerequisite activity.
- Create an extension library, as described in the To Create an Extension Library prerequisite activity.
Step 1: Creating a Push Notification Definition
To configure the system to send push notifications to the SignalR hub, create a push notification definition as follows:
- On the Push Notifications (SM302000) form, in the Destination Name box, type the name of the target notification destination, such as TestSignalR.
- In the Destination Type box, select SignalR Hub. The Address box is filled in automatically with PushNotificationsHub.
- For each generic inquiry for which you want MYOB Acumatica to send notifications on changes in the inquiry results, do the following on
the Generic Inquiries tab:
- On the table toolbar of the Inquiries table, click Add Row. The new row has the Active check box selected.
- In the Inquiry Title column of the added row, select the generic inquiry for which you want MYOB Acumatica to send notifications.
- Do one of the following:
- If you want the system to send push notifications for changes in
any field in the results of the generic inquiry, select the
Track All Fields check box in the
added row.Tip:If all fields in the results of the generic inquiry are tracked, the system produces push notifications for changes of any field in the results of the generic inquiry, which can cause overflow of the push notification queue. If you need to track only particular fields in the results of the generic inquiry, push notifications for changes in other fields are useless for you but consume system resources. Therefore, we recommend that you specify particular fields to be tracked in the Fields table.
- If you need to track only particular fields in the results of
the generic inquiry, while the row of the
Inquiries table is still selected, do
the following in the Fields table for
each field that you need to track:
- On the table toolbar, click Add Row.
- In the Table Name column of the added row, select the name of the table that contains the field that the system should track.
- In the Field Name column, select the name of the field that the system should track.
- If you want the system to send push notifications for changes in
any field in the results of the generic inquiry, select the
Track All Fields check box in the
added row.
- On the form toolbar, click Save.
Step 2: Connecting to the SignalR Hub
To connect to the SignalR hub, do the following:
- In a project of your MYOB Acumatica extension library, set up a Basic authentication token to authenticate the
application in MYOB Acumatica, as shown in the following
code.
using System; using System.Collections.Generic; using System.Text; using Microsoft.AspNet.SignalR.Client; class Program { static void Main(string[] args) { var login = "admin"; var tenant = "Tenant"; var password = "123"; // Set up a Basic authentication token var basicAuthToken = Convert.ToBase64String( Encoding.UTF8.GetBytes(login + "@" + tenant + ":" + password)); }
- Connect to an instance of MYOB Acumatica, as shown in the following
code.
class Program { static void Main(string[] args) { ...
//Connect to an Acumatica ERP instance var connection = new HubConnection("http://localhost:8081/AcumaticaDB/"); connection.Headers.Add("Authorization", "Basic " + basicAuthToken); } }
- Create a proxy to the SignalR hub, based on the name of the hub that was
specified in the Destination Name box when the push
notification was defined on the Push Notifications
(SM302000) form in Step
1.
class Program { static void Main(string[] args) { ...
//Create a proxy to hub //Use "PushNotificationsHub" as the address of the hub var myHub = connection.CreateHubProxy("PushNotificationsHub"); connection.Start().ContinueWith(task => { if (task.IsFaulted) { Console.WriteLine( "There was an error during open of the connection:{0}", task.Exception.GetBaseException()); } else { //Instead of "TestSignalR", specify the name //that you specified on the Push Notifications form myHub.Invoke<string>("Subscribe", "TestSignalR").Wait(); } }).Wait(); } }
- Define the class for a notification, as shown in the following code. For details
on the format of the push notifications, see Push Notifications: Format.
public class NotificationResult { public object[] Inserted { get; set; } public object[] Deleted { get; set; } public string Query { get; set; } public string CompanyId { get; set; } public Guid Id { get; set; } public long TimeStamp { get; set; } public Dictionary<string, object> AdditionalInfo { get; set; } }
- Implement processing of notifications. The following code displays the number of inserted and updated records specified in the notification in the console application window.
-
class Program { static void Main(string[] args) { ...
//Process the notifications myHub.On<NotificationResult>("ReceiveNotification", nr => { Console.WriteLine("Inserted {0}", nr.Inserted.Length); Console.WriteLine("Deleted {0}", nr.Deleted.Length); }); Console.Read(); connection.Stop(); } }
- Build and test the project.