Reputation: 89
I have an application that makes calls from the client using JQuery to a Web API controllers, but then from such controller is makes a call to another server, where another controller picks up and does all the data logic (insertions, etc..) so basically (two different solutions and totally separate, no dependencies)
Client
Web API (this live in http://localhost:5020/)
-----------------
Some Server API here (this lives in http://localhost:4566)
Data Layer
SQL
So Web API makes calles to some server and saves or retrieves data.
I need to be able to add SinalR when something is saved to one of the databases on the other server. How can I design this so I get notifications something was saved in the client side since there are no dependencies?.
Do i add the HUBs and what not on the receiving server or on the client server, a bit confuse how that would work.
I would appreciate any clarification.
Upvotes: 2
Views: 3777
Reputation: 8276
I usually use the method explained in this answer.
This will allow you to have hubs which can be invoked from frontend (JS) and backend (C#) as well. Basically for the backend (C#) calls use a hubcontext. As explained by the SignalR team here too.
Simple code (in your hub class):
public class YourHubClass: Hub
private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<YourHubClass>();
// ...
public static void Send(string message) {
hubContext.Clients.All.addMessage(message);
}
}
Now you can call the static method in your controller:
YourHubClass.Send("Hello!");
And the clients will receive the message by the event addMessage(message)
Upvotes: 2