I developed a webpage that connects to the signalR hacker in jquery code with angular. Clients are sent messages when the sqldependency.onchange event sqldependency.onchange , however, for each client connected to the message, each is duplicated. Thus, if two clients are connected, each client receives two messages, etc.
Here are the steps:
- connect two clients to a hub
- Make a database change
- sqlDependency.onchange fires
- Call concentrator function
clients.all.renewProducts() - Restore data repository reset dependency.
- Client Console:
"Database SQL Dependency change detected: Update" app.js:44:12 (Twice)
Hub.cs
public static void SignalRGetData(string data) { IHubContext context = GlobalHost.ConnectionManager.GetHubContext<SignalRGetData>(); context.Clients.All.renewData(data);
DataRespository.cs _dependency_OnChange
public void _dependency_OnChange(object sender, SqlNotificationEventArgs e) { if(e.Info == SqlNotificationInfo.Update) { ProductHub.GetProducts("Database SQL Dependency change detected: " + e.Info); }
}
GetData STRONG>
public IEnumerable<ProductInventoryDetail> GetData() { using(var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DynamicPricing"].ConnectionString)) { conn.Open(); var reposQuery = "SELECT [ID], [Program] FROM [DBO].[Detail]"; using(SqlCommand cmd = new SqlCommand(reposQuery, conn)) {
Angular javascript
// Apply jQuery SignalR operations to Angular app.value('$', $); app.factory('signalRService', ['$', '$rootScope', function ($, $rootScope) { var proxy = null; var initialise = function () { // Get Connection to SignalR Hub var connection = $.hubConnection(); // Create a Proxy proxy = connection.createHubProxy('SignalRData'); // Publish the event when server has a push notification proxy.on('renewProducts', function (message) { console.log("Database SQL Dependency change detectedgnalRGetData: " + message); $rootScope.$emit('renewProducts', message); }); // Start Connection connection.start().done(function () { console.log("Conenction Ready - invoke proxy"); proxy.invoke('SignalRGetData'); }); }; return { initialise: initialise } }]);
source share