I have an ASP.NET Web API that exposes some REST methods. The client browser authenticates with the API using a Login screen. I would like to broadcast a message (javascript alert) to all clients when another client authenticates with the API. No need for the client to invoke a method on the server, just the server calling a single method on the client to display a Javascript Alert.
My Hub class:
public class MyHub : Hub
{
public void BroadcastMessage(string userName)
{
Clients.All.notify(userName);
}
}
Inside my CustomOAuthProvider classs where user authentication takes place:
if (userAuthenticated)
{
var signalRContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
signalRContext.Clients.All.notify(context.UserName);
}
Here is my index.html file with the relevant scripts:
<script src="scripts/jquery-2.2.2.min.js"></script>
<script src="scripts/jquery.signalR-2.2.0.js"></script>
<script src="http://localhost:49834/signalr/hubs"></script>
<script>
(function ($) {
$(function () {
var hubProxy = $.connection.myHub;
hubProxy.client.notify = function (username) {
alert(username + ' has logged in');
};
$.connection.hub.start();
});
})(jQuery);
</script>
Note that the path to the dynamically generated SignalR Hubs script has localhost:49834. This is the url to my web api. I included this because the client is running in a Node.js http-server and is not part of the ASP.NET Web Api project.
When I run this I get no errors on the console and no alerts. I login with one user then open a separate browser and login with a second user. I am not sure why I am not getting a Javascript Alert
with the authenticated username.