2

For the last few days I have been trying to implement SignalR into my AngularJS/WebAPI application.

I have been able to successfully send/receive messages from client to client, however when I push messages purely from the Server, none of the clients receive any messages.

I have seen many people having the same problem, the answer always seems to be using GlobalHost.ConnectionManager.GetHubContext which I have been implementing, without error, however the clients still don't receive any of the messages.

I thought that perhaps it's because WebAPI calls are asynchronous and and therefore takes place on a different thread, but I can't be sure. Please could someone have a look and tell me what I'm doing wrong.

This is my Hub Class:

 public class ChatHub : Hub
{

    public void Send(string name, string message)
    {
        // Call the broadcastMessage 
        Clients.All.broadcastMessage(name, message);
    }

    public void RunMe()
    {
        System.Diagnostics.Debug.WriteLine("Client Started");
    }

    public static void Notify(string name, string message)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        hubContext.Clients.All.broadcastMessage(name, message);
    }

}    

This is the Angular Controller in Javascript:

$scope.chat = $.connection.chatHub;

$scope.chat.client.broadcastMessage = function (name, message) {
    $scope.$apply(function () {
        var scope = angular.element($('#discussion')).scope();
        scope.chatMessage = message;

        alert(message);
    });
};

$.connection.hub.start()
.done(function ()
{
    console.log('Now connected, connection ID=' + $.connection.hub.id);
    $scope.chat.server.runMe();
})
.fail(function(){ console.log('Could not Connect!'); });

$('#sendmessage').click(function () {
    $scope.chat.server.send("SERVER", $('#inputMessage').val());
});    

This is the Controller that is trying to notify the clients from the server

    public class UserController : ApiController
{
    #region METHODS

    [ActionName("Create")]
    [HttpPost]
    public HttpResponseMessage Create(JObject parameters)
    {

        //DYNAMIC DATA
        dynamic data = parameters;

        //CHECK IF CALL FAILED
        if (data == null)
            return Request.CreateResponse(HttpStatusCode.InternalServerError, "Request is null");

        //PERFORM REQUEST 
        using (var svc = new UserService())
        {
            //SET Parameters
            String Username = data.Username;
            String Password = data.Password;

            //NOTIFY USERS
            ChatHub.Notify("SERVER", "SERVER MESSAGE");

            //CREATE Response
            var response = svc.Create(Username, Password);

            //RESPOND
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }
    }

}

So just to reiterate, when the "sendmessage" button is clicked on my UI, it sends a message to the server which is then received again by the clients, this works 100%,

However when I call the static Notify method from the Controller None of the clients receive any messages.

Calling the function does not return any errors.

Please could someone help me!

7
  • why are you using jquery in the controllers? create a signalr service wrapper Commented Feb 3, 2015 at 13:26
  • @jack.the.ripper Thanks for the reply. Do you have a small example using the signalr service wrapper? I am new to this and just cannot figure it out. Thanks in advance.
    – Stephan
    Commented Feb 4, 2015 at 13:29
  • Pretty sure he is talking about angular services; does this help: stackoverflow.com/questions/20139127/… ? Commented Feb 14, 2015 at 3:36
  • stackoverflow.com/questions/20561196/… Commented Feb 14, 2015 at 3:51
  • hmm.. actually I tried this just now, and it works for me Commented Feb 14, 2015 at 4:07

2 Answers 2

0

    <!--Reference the SignalR library. -->
    <script src="Scripts/jquery.signalR-2.2.0.min.js"></script>

Check your jQuery.signalR version.

0

If you are using dependency injection, the example at ASP.NET is wrong, you have to set your GlobalHost.DependendcyResolver in the the Global.asax file. not in the startup class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.