Question

Here is the Hub Class

public class NotificationHub:Hub
    {
        public void Hello(string msg)
        {
            Clients.All.helloEveryone(msg);
        }
    }

Button Click for server side call in the document.ready function

   $.connection.hub.start().done(function () {
                $('#setnotification').click(function () {
                    var notifier = $.connection.notificationHub;
                    notifier.server.hello('World!');
                });

And here is the call to all clients

 <script type="text/javascript">
        var notifier = $.connection.notificationHub;
        notifier.client.helloEveryone = function (msg) {
            $('#chatWindow').text(msg);
        }
</script>

But the problem is When I have two browsers Opened I can see that only active browser shows the notification.Not Another.

Was it helpful?

Solution

Please have a look at http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-getting-started-with-signalr-20-and-mvc-5

You will notice you are binding your JavaScript client object each time the button is clicked which most likely is causing issues. Recommend having a look at how the example at this URL instantiates the client side and go from there. Notice they perform the $.connection.hub.start() in document ready, not for each chat request

Is the problem with declaring the same variable name notifier in both the button click event and in global scope? Depending on where your code sits in the DOM, you should be able to just change the line in setnotification client to notifer.server.hello to get it to work. Just call the global notifier declared in your DOM. Don't reestablish a connection This could work as just a simple example:

<script type="text/javascript">
    var notifier = $.connection.notificationHub;
    notifier.client.helloEveryone = function (msg) {
        $('#chatWindow').text(msg);
    });
    $(function(){
        notifier.start().done(function(){
            $("#setnotification").click(function(){
                notifier.server.hello('World!');
            });
        });
    });
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top