Using SignalR with SqlDependency to push database updates

Willem D'Haeseleer picture Willem D'Haeseleer · Mar 26, 2012 · Viewed 7.3k times · Source

Is is possible to use SignalR in combination with SqlCacheDependency (or SqlDependency) to push database updates directly to the browser ? Maybe there is an other way to achieve this functionality ?

The only thing i can get working now includes having to call addMessage from the async call that does an update to the datase, but that doesn't really cover updates from different sources ( for example a background service that updates the table ).

Answer

Ian Mercer picture Ian Mercer · Mar 27, 2012

You should be able to use the OnChange event on a SQLDependency. In your event handler you can send a message over SignalR. Since you will be calling into your Hub from outside you'll need to use the technique shown at the bottom of the documentation here :

using SignalR.Infrastructure;

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<MyHub>(); 
...