Websocket utility for NestJS based on WS
npm i nestjs-websocket
The url param that websocket module expects should be a string, for example:
{
url: 'ws://localhost:3000',
}
Use WebSocketModule.forRoot
method with String param:
import { WebSocketModule } from 'nestjs-websocket'
@Module({
imports: [
WebSocketModule.forRoot({
url: 'ws://localhost:3000',
}),
],
...
})
class MyModule {}
WebSocketModule.forRootAsync
allows you, for example, inject ConfigService
to use it in Nest useFactory
method.
useFactory
should return an object with String param.
import { WebSocketModule } from 'nestjs-websocket'
@Injectable()
@Module({
imports: [
WebSocketModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => {
return {
url: 'ws://localhost:3000'
}
},
}),
],
...
})
class MyModule {}
This package exposes a getWebSocketToken()
function that returns a prepared injection token based on the provided context.
Using this token, you can easily provide a mock implementation of the WS using any of the standard custom provider techniques, including useClass, useValue, and useFactory.
const module: TestingModule = await Test.createTestingModule({
providers: [
MyService,
{
provide: getWebSocketToken(),
useValue: mockProvider,
},
],
}).compile();
See Changelog for more information.
Contributions welcome! See Contributing.
Licensed under the Apache 2.0 - see the LICENSE file for details.