#WebSocketGateway forwardRef

4 messages · Page 1 of 1 (latest)

frosty reef
#

Hello everyone! I have an issues with forwarding ref for WebSocketGateway and services.

I have global app-module

@Module({
  exports: [...modules, MyWebSocketGatewayModule, SpecificServiceThatUseWebsocket],
  imports: [...modules, forwardRef(() => MyWebSocketGatewayModule), forwardRef(() => SpecificModuleThatUseWebsocket)],
})
export class AppModule {}

I have MyWebSocketGatewayModule

@Module({
  imports: [...],
  providers: [
    MyWebSocketGateway,
    SocketService,
  ],
  exports: [MyWebSocketGateway, SocketService],
})
export class MyWebSocketGatewayModule {}

I have SpecificModuleThatUseWebsocket:

@Module({
  imports: [
    SocketService,
  ],
  controllers: [
    ...
  ],
  providers: [
    SpecificServiceThatUseWebsocketService,
    SocketService,
  ],
})
export class SpecificModuleThatUseWebsocket {
}

I have MyWebSocketGateway:

@WebSocketGateway()
export class WebsocketsGateway {
  socket: ServerSocket;
  @WebSocketServer() server: Server;

  constructor(
    private specificServiceThatUseWebsocketService: SpecificServiceThatUseWebsocketService,
  ) {}
}

I have SocketService:

@Injectable()
export class SocketService {
  @WebSocketServer() server: Server;

  constructor(
    private readonly websocketsGateway: MyWebSocketGateway,
  ) {
    this.socket = websocketsGateway.server;
  }
}
#

Nest throw error like
ERROR [ExceptionHandler] A circular dependency has been detected inside MyWebSocketGatewayModule. Please, make sure that each side of a bidirectional relationships are decorated with "forwardRef()". Note that circular relationships between custom providers (e.g., factories) are not supported since functions cannot be called more than once.

When I try to add socketService for SpecificServiceThatUseWebsocketService:

@Injectable()
export class SpecificServiceThatUseWebsocketService{
  constructor(private readonly socketService: SocketService,
  ) {
    super();
  }

I've tried to use forwardRef inside @Inject decorator instead of forwarding in modules

#

any idea how I can handle it?

frosty reef
#

up