JavaScript How Use SingalR with multiple controllers in angular js signalr with multiple servers,signalr client with multiple connections,signalr

I am going to use SingalR with angular; my requirement is that just only one time gets a connection id as a singleton pattern and used all over a single app because in my case browser will load first time so that clientid maintains for the remaining views in the controller.

So the first problem is that I created factory which is shown below, but when I am using this factory I am getting the error:

undefined factory.Uncaught ReferenceError: signalRHubProxy is not defined(…).

I have done every include js file on index every thing setup on server side.

In given code my questions that how can I do. Any sample code?

  1. why not getting factory methods.
  2. How can I utilize addChatMessage listener on every controller when ever any message come its activate.

Hope all things cleared.

One more thing if I am using this code working fine but my scenario is that which I defined above.

If I am on index page simple like this.

index.html

$(function () {
            $.connection.hub.url = Config._HubConnectionUrl;
            $.connection.hub.logging = true;

            var chat = $.connection.communicationHub;            
            chat.client.addChatMessage = function (userId, message) {
                debugger
                console.log("wajih");
            };

            $.connection.hub.start().done(function () {
               debugger
               $("#clientId").text($.connection.hub.id);             
            });
        });

When I am sending any message addmessage event trigger, but the above I want to utilize on following structure on multiple controllers. Here is my code.

RealTimeFactory.js Factory:-
 'use strict';

var singalR = angular.module('signalRHubProxy', []);

singalR.factory('signalRHubProxy', function ($rootScope) {
    function signalRHubProxyFactory(serverUrl, hubName, startOptions, $rootScope) {
        var connection = $.hubConnection();
        var proxy = connection.createHubProxy(hubName);
        connection.start(startOptions).done(function () {
              debugger
        });

        return {
            on: function (eventName, callback) {
                proxy.on(eventName, function (result) {
                    $rootScope.$apply(function () {
                        if (callback) {
                            callback(result);
                        }
                    });
                });
            },
            off: function (eventName, callback) {
                proxy.off(eventName, function (result) {
                    $rootScope.$apply(function () {
                        if (callback) {
                            callback(result);
                        }
                    });
                });
            },
            invoke: function (methodName, callback) {
                proxy.invoke(methodName)
                    .done(function (result) {
                        $rootScope.$apply(function () {
                            if (callback) {
                                callback(result);
                            }
                        });
                    });
            },
            connection: connection
        };
    };

    return signalRHubProxyFactory;
});

My Global controller:-

var app = angular.module('sgApp.controllers', ['signalRHubProxy']);

Now I want use this factory in my controllers but given error as I mentioned above.

ChatController.js

app.controller('ChatCtrl', function ($rootScope, $scope, signalRHubProxy) {

    //var clientHubProxy = signalRHubProxy(
    //   signalRHubProxy.defaultServer, 'communicationHub',
    //       { logging: true });

    //clientHubProxy.on('addChatMessage', function (data) {
    //    debugger
    //    var x = clientHubProxy.connection.id;
    //});

});

HomeController.js

app.controller('HomeCtrl', function ($rootScope, $scope, signalRHubProxy) {

    //var clientHubProxy = signalRHubProxy(
    //   signalRHubProxy.defaultServer, 'communicationHub',
    //       { logging: true });

    //clientHubProxy.on('addChatMessage', function (data) {
    //    debugger
    //    var x = clientHubProxy.connection.id;
    //});

});

Here is ServerSide Code HUB:-

public class CommunicationHub : Hub
    {
        public void SendTo(string userId, string message)
        {
            Clients.Client(userId).addChatMessage(userId, message);
        }
    }
Answer:1

I'm new to javascript and SVG and I have no graphical programming background and this is my first project using all of those. So I started to make a custom element just like Mike Goodwin answer ...

I'm new to javascript and SVG and I have no graphical programming background and this is my first project using all of those. So I started to make a custom element just like Mike Goodwin answer ...

I want to make use of one html tag into my javascript but don't know how to make use of that. For ex: <p>An absolute URL: <a href="https://www.w3schools.com">W3Schools</a></p>...

I want to make use of one html tag into my javascript but don't know how to make use of that. For ex: <p>An absolute URL: <a href="https://www.w3schools.com">W3Schools</a></p>...

  1. html into javascript variable
  2. insert html into javascript
  3. import html into javascript
  4. html input into javascript variable
  5. convert html into javascript
  6. add html into javascript
  7. convert html into javascript string
  8. insert html into javascript variable
  9. embed html into javascript
  10. html into string javascript
  11. inject html into javascript
  12. turn html into javascript string
  13. html form into javascript variable
  14. turn html into javascript
  15. write html into javascript
  16. html code into javascript
  17. load html into javascript variable
  18. include html into javascript
  19. load html into javascript
  20. put html into javascript

I have developed and packaged an offline Cordova-based HTML5 App for Android. It works fine, and some of the resources are accessed locally without an internet connection while other require an ...

I have developed and packaged an offline Cordova-based HTML5 App for Android. It works fine, and some of the resources are accessed locally without an internet connection while other require an ...

I'm trying to prove that a simple function call such as window.alert(); is valid EcmaScript 2016 (7th Edition) grammar. Working backward, with the expectation this is an ExpressionStatement, I see ...

I'm trying to prove that a simple function call such as window.alert(); is valid EcmaScript 2016 (7th Edition) grammar. Working backward, with the expectation this is an ExpressionStatement, I see ...

  1. javascript function call itself
  2. javascript function call another function
  3. javascript function call vs apply
  4. javascript function call in html
  5. javascript function call overhead
  6. javascript function call method
  7. javascript function call not working
  8. javascript function call with parameters
  9. javascript function call performance
  10. javascript function call apply
  11. javascript function call failed null
  12. javascript function call stack
  13. javascript function call on load
  14. javascript function call syntax
  15. javascript function call by reference
  16. javascript function call two parentheses
  17. javascript function call on page load
  18. javascript function call on button click
  19. javascript function call without parentheses
  20. javascript function call inside function