Search code examples
javascriptangularjsnode.jsmeanjs

'Unknown Provider' trying to Include angular-websocket in a MeanJS v0.4.1 app


I am having a bit of difficulty using angular-websocket in a controller in a MeanJS application I am working on.

My Application is based on MeanJS v0.4.1.

I first installed it with:

    bower install angular-websocket --save    

This created the directory /public/lib/angular-websocket

Next I added it to /config/assets/default.js

    lib: {
      css: [
        ...
      ],
      js: [
        ...
        'public/lib/angular-websocket/angular-websocket.js'
      ],
      tests: ['public/lib/angular-mocks/angular-mocks.js']
    },

In my /modules/core/client/app/config.js file I have added it as a dependency:

    var applicationModuleVendorDependencies = [
        ...
        'angular-websocket'
    ];

And finally in my angular module itself,

    angular.module('somemodule').controller('ModulenameController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'SomeModule', 'ngWebSocket',
        function ($scope, $http, $stateParams, $location, Authentication, SomeModule, ngWebSocket) {

When I go to view my page I can see in the "Sources" tab of Chrome's Developer tools that it is included as a source,

I am trying to use this file with something like this in my controller:

    var dataStream = ngWebSocket('wss://www.somesite.com/realtime');

    dataStream.onMessage(function(message) {
        console.log("dataStream Message: " + message);
        $scope.orderBook = message;
    });

    dataStream.send('{"op":"subscribe", "args":"someargument"}');

However, my console shows the following error:

Error: [$injector:unpr] Unknown provider: ngWebSocketProvider <- ngWebSocket <- ModuleNameController

Some of the things I have tried:

  1. Changing The Reference Name
    As per the documentation (https://www.npmjs.com/package/angular-websocket)

    angular.module('YOUR_APP', [
      'ngWebSocket' // you may also use 'angular-websocket' if you prefer
    ])
    

I've tried using 'ngWebSocket', 'angular-websocket', but I still get the same issue.

  1. I've tried looking through my code to see if maybe this was being redefined as the angularJS documentation here:

    https://docs.angularjs.org/error/$injector/unpr

states that a cause of this error could be 'redefining a module using the angular.module API'.

Any help would be greatly appreciated.


Solution

  • The factory is actually named $websocket, so you should do as follows:

     angular.module('somemodule').controller('ModulenameController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'SomeModule', '$websocket',
        function ($scope, $http, $stateParams, $location, Authentication, SomeModule, $websocket) {