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

SocketIoModule.forRoot with factory ?

Open davidquintard opened this issue 4 years ago • 8 comments

Hi there i'm using this module like this:

const config: SocketIoConfig = { url: endpoint.socket, options: {
    query: {
      token: localStorage.getItem('access_token')
    }
}};
...
SocketIoModule.forRoot(config),

But i need to use Ionic storage instead of localstorage.

I'm wondering if there is a factory method to use like @auth0/angular-jwt:

export function jwtOptionsFactory(storage: Storage) {
  return {
    tokenGetter: () => {
      return storage.get('access_token');
    },
    whitelistedDomains: endpoint.jwtWhitelist
  }
....
JwtModule.forRoot({
      jwtOptionsProvider: {
        provide: JWT_OPTIONS,
        useFactory: jwtOptionsFactory,
        deps: [Storage]
      }
    }),
}

Regards

davidquintard avatar May 10 '20 11:05 davidquintard

No factory to use ?

davidquintard avatar May 18 '20 18:05 davidquintard

same issue

almishlab8 avatar May 19 '20 02:05 almishlab8

I'm also interested in having this. In addition, the capability to define whitelisted domains/origins is useful for security checks

dsebastien avatar Jul 03 '20 08:07 dsebastien

Could you give some more information? I suppose this is to asynchronously retrieve things? Or is it for dependency injection into the factory arguments? Or both? :) If it's for async wouldn't you want to use Observables/async instead of a factory pattern?

Helveg avatar Jul 04 '20 08:07 Helveg

Could you give some more information? I suppose this is to asynchronously retrieve things? Or is it for dependency injection into the factory arguments? Or both? :) If it's for async wouldn't you want to use Observables/async instead of a factory pattern?

For my purpose, it's for dependency injection (ionic storage).

davidquintard avatar Jul 04 '20 09:07 davidquintard

Actually i've worked around this by following advice in another ticket. I don't use the .forRoot anymore, but a service with a "connect" method that I only invoke when the user is logged into my application.

That way, I can also provide the configuration lazily and thus I don't have this issue anymore.

The thing is that in most systems that require authentication, the WebSocket connection should only be established while the user is authenticated, so I suppose it would be nice to have support for this scenario (but indeed it's a separate feature request).

dsebastien avatar Jul 04 '20 09:07 dsebastien

Na verdade, resolvi isso seguindo o conselho de outro tíquete. Eu não uso mais o .forRoot, mas um serviço com um método de "conexão" que só invoco quando o usuário está logado em meu aplicativo.

Dessa forma, também posso fornecer a configuração preguiçosamente e, portanto, não tenho mais esse problema.

O fato é que na maioria dos sistemas que requerem autenticação, a conexão WebSocket só deve ser estabelecida enquanto o usuário está autenticado, então suponho que seria bom ter suporte para este cenário (mas na verdade é uma solicitação de recurso separada).

Actually i've worked around this by following advice in another ticket. I don't use the .forRoot anymore, but a service with a "connect" method that I only invoke when the user is logged into my application.

That way, I can also provide the configuration lazily and thus I don't have this issue anymore.

The thing is that in most systems that require authentication, the WebSocket connection should only be established while the user is authenticated, so I suppose it would be nice to have support for this scenario (but indeed it's a separate feature request).

I have this same problem, I need to start my websocket only after the user is logged in, but when I try to create a service and inject in AppModule it is giving the same error as this issue #76, you could not post your solution in a fiddler or in a repository ?

jeffersonveloso avatar Nov 06 '20 14:11 jeffersonveloso

@dsebastien Tienen algún ejemplo en github? mi problema es cuando recargan la pagina (F5)

mtenorio-cmp avatar Feb 12 '21 20:02 mtenorio-cmp