To Connect to the SignalR Hub
If you want your external application to receive push notifications from MYOB Acumatica or an MYOB Acumatica Framework-based application but you cannot publish a web hook (for example, for security reasons), the system can send notifications to the SignalR hub, from which any connected application can receive the notifications.
To connect the external application to the SignalR hub, follow the instructions in this topic.
To Connect to the SignalR Hub
- In your external application, set up a Basic authentication token to
authenticate the application in MYOB Acumatica or an MYOB Acumatica Framework-based application, as shown in the following
code.
using System; using System.Text; 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)); } }
- In your external application, connect to an instance of MYOB Acumatica or an MYOB Acumatica Framework-based application, as shown in the following
code.
using System; using System.Text; using Microsoft.AspNet.SignalR.Client.Hubs; class Program { static void Main(string[] args) { ... //Connect to an MYOB Acumatica instance var connection = new HubConnection("http://localhost:8081/AcumaticaDB/"); connection.Headers.Add("Authorization", "Basic "+basicAuthToken); } }
- In your external application, 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.
using System; using System.Text; using Microsoft.AspNet.SignalR.Client.Hubs; 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(); } }
- In your external application, define the class for a notification, as shown in
the following code. For details on the format of the push notifications, see
Push Notification 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; } }
- In your external application, process notifications. The following code displays
the number of inserted and updated records specified in the notification in the
console application
window.
using System; using System.Text; using Microsoft.AspNet.SignalR.Client.Hubs; 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(); } }
- Compile and test your application.