Greetings, I am writing on angular 2 and I cannot figure out how to organize the connection to соккетам in a separate service, so that I can work with its methods in different classes.

The problem is that when creating a service and importing more than once in a project, соккеты connected as many times as there were imports for the project.

Perelazilil all Internet, there are only connection examples that do not solve the problem of multi-import project.

Thanks to everyone who responded.

    2 answers 2

    This is most likely happening because you specify the service in the providers block for each component separately. In this case, a separate service instance will be created for each component. Try adding your service to the providers section of the main component. Details: https://stackoverflow.com/questions/36198785/how-do-i-create-a-singleton-service-in-angular-2

      For 2.0 Release, respectively, the dependency for services should now be written once in the NgModule:

      https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-root-component-or-module