angular-laravel-echo
angular-laravel-echo copied to clipboard
Multiple "socket.io" connections
How to implement multiple "socket.io" connections?
NgxLaravelEchoModule.forRoot(echoConfig1) // code line 1
NgxLaravelEchoModule.forRoot(echoConfig2) // code line 2
The final effect is "code line 2"
That is not possible since the Service also tracks state, so you'd need to multiple instances of the service to be able to do that which breaks the Angular DI. The only way I can think of is by using custom factory functions to instantiate 2 different instances and bind those to 2 different InjectionTokens, so you'd get something like:
export const echoFactory1 = (ngZone: NgZone) => new EchoService(ngZone, echoConfig1);
export const echoFactory2 = (ngZone: NgZone) => new EchoService(ngZone, echoConfig2);
export const echoService1Token = new InjectionToken<EchoService>('EchoService1');
export const echoService2Token = new InjectionToken<EchoService>('EchoService2');
Inside the (app) module providers array you'd have:
{provide: echoService1Token, deps: [NgZone], useFactory: echoFactory1},
{provide: echoService2Token, deps: [NgZone], useFactory: echoFactory2},
And in the constructor you can inject it like this:
constructor(@Inject(echoService1Token) echoService1: EchoService,
@Inject(echoService2Token) echoService2: EchoService) {
}
Above are just (untested) examples based on docs and experience. Keep in mind that this solution is way out of the "regular" use case for this module and I can't guarantee if it'll even work.
Just out of curiosity why would you want 2 connections to Laravel Echo since that'd probably also means you'd be connecting to 2 completely separate Laravel backends?