Just starting out with SignalR and attempting to set up a notification for a specific event triggered by an API.
What I've attempted:
Hub:
public class NotificationHub : Hub
{
private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
public static void Send(string content)
{
hubContext.Clients.All.addMessage(content);
}
}
Controller:
//static event from external API
public static void onTick(Tick TickData)
{
if(TickData.InstrumentToken == buy.InstrumentToken)
{
NotificationHub.Send(TickData.Bid);
}
}
How can I display the message triggered by this condition in the View?
Attempted View:
$(document).ready(function () {
var conn = $.connection.NotificationHub;
conn.client.addMessage = function (message) {
alert(message);
};
});
Is there anything else I need to do to make this work?
Edit:
Ashley's response brought me closer, however, a couple of things were missing such as:
`connection.NotificationHub` should be `connection.notificationHub`
The order of the JavaScript files references should be:
1. jquery-1.10.2.min.js
2. jquery.signalR-2.1.0.min.js
3. signalr/hubs
But now when executing it, it enters the `.fail(function()` and the console shows an error: . Failed to load resource: net::ERR_CONNECTION_REFUSE
Please provide guidance. Thank you in advance.