ngx-socket-io icon indicating copy to clipboard operation
ngx-socket-io copied to clipboard

Cannot use with multiple socket

Open shellking4 opened this issue 2 years ago • 1 comments

I'm having an issue with the package. I want to use multiple sockets. I followed the guide on the github repo for that.

core.mjs:8416 ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AdminModule)[ReRegistrationService -> WrappedSocket -> WrappedSocket -> WrappedSocket -> WrappedSocket]: 
  NullInjectorError: No provider for WrappedSocket!
NullInjectorError: R3InjectorError(AdminModule)[ReRegistrationService -> WrappedSocket -> WrappedSocket -> WrappedSocket -> WrappedSocket]: 
  NullInjectorError: No provider for WrappedSocket!
    at NullInjector.get (core.mjs:7509:27)
    at R3Injector.get (core.mjs:7930:33)
    at R3Injector.get (core.mjs:7930:33)
    at R3Injector.get (core.mjs:7930:33)
    at R3Injector.get (core.mjs:7930:33)
    at injectInjectorOnly (core.mjs:633:33)
    at Module.ɵɵinject (core.mjs:637:60)
    at Object.ReRegistrationService_Factory [as factory] (re-registration.service.ts:12:35)
    at R3Injector.hydrate (core.mjs:8031:35)
    at R3Injector.get (core.mjs:7919:33)
    at resolvePromise (zone.js:1214:31)
    at resolvePromise (zone.js:1168:17)
    at zone.js:1281:17
    at _ZoneDelegate.invokeTask (zone.js:409:31)
    at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:23891:28)
    at _ZoneDelegate.invokeTask (zone.js:408:60)
    at Object.onInvokeTask (core.mjs:24189:33)
    at _ZoneDelegate.invokeTask (zone.js:408:60)
    at Zone.runTask (zone.js:178:47)
    at drainMicroTaskQueue (zone.js:588:35)

It is throwing a dependency injection error. I can't find a way to fix it.


@Injectable()
export class ReRegistrationService extends Socket {

  public data$ = this.socket.fromEvent<any>('requestsList');

  constructor(
    private httpClient: HttpClient,
    private socket: Socket,
    private commonService: CommonService,
) {
    super({
      url: `${apiEndpoints.apiServerUrl}/api/v1/requests`,
      options: {}
    }) 
  }

shellking4 avatar Mar 28 '23 07:03 shellking4

What you probably want to do is this, but please take another careful look at https://github.com/rodgc/ngx-socket-io#using-multiple-sockets-with-different-end-points

@Injectable()
export class SomeSocket extends Socket {
  constructor() {
    super({
      url: `${apiEndpoints.apiServerUrl}/api/v1/requests`,
      options: {}
    }) 
  }
}

@Injectable()
export class ReRegistrationService {
  public data$ = this.socket.fromEvent<any>('requestsList');

  constructor(
    private httpClient: HttpClient,
    private socket: SomeSocket,
    private commonService: CommonService,
  ) {}
}

Don't forget to add SomeSocket to a providers array of a module.

The class that extends Socket should not be used for anything else than the constructor call to super, then be provided in an Angular module, and then be injected in the constructor of another service that has access to that provider.

Helveg avatar Sep 04 '23 18:09 Helveg

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jun 15 '24 02:06 github-actions[bot]