[ ] Regression
[ ] Bug report
[ ] Feature request
[ X ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
There is no way to pass or request a provider to be passed to our adapter
So basically i have a dependency on getting Redis service into my adapter because i add user keys at connection level and i wanted to bind a middleware on adapter using that initialized provider.
export class RedisIoAdapter extends IoAdapter {
constructor(maybe here the service ?) {}
createIOServer(port: number, options?: any): any {
const server = super.createIOServer(port, options);
server.adapter(redisAdapter);
//and use it here
server.use( myRedisService....)
return server;
}
}
If this is available to be injected, i'll be glad to know how because docs don't cover too much about this
Nest version: > 5
For Tooling issues:
- Node version: 11.10.0
- Platform: Linux
Others:
I had the same Problem. Here is my solution:
My Redis Adapter class: (ConfigService is my service to pull enviroment variables)
export class RedisIoAdapter extends IoAdapter {
private redisAdapter;
constructor(
private readonly app: NestFastifyApplication,
private readonly configService: ConfigService
) {
super(app);
this.redisAdapter = redisIoAdapter({
host: this.configService.getString('REDIS_HOST'),
port: this.configService.getNumber('REDIS_PORT')
});
}
createIOServer(port: number, options?: ServerOptions): any {
const server = super.createIOServer(port, options);
server.adapter(this.redisAdapter);
return server;
}
}
And then my "main.ts":
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ trustProxy: true })
);
const configService = app.get(ConfigService);
app.useWebSocketAdapter(new RedisIoAdapter(app, configService));
await app.listen(3000);
}
bootstrap();
Works well for me. Hope I could help!
@Avejack Hi thanks for replying, your answer is good but in my case i want to have redis instance connected right before adapter being called.
I have a dependecy loader created that does the work.
The use case is that my microservice instances create "domain keys" that each time they reconnect they have to delete.
You're answer is good but what i really was looking for is nestjs provide a Service injected to adapter (a way of loading it first)
@underfisk you could get the service in the bootstrap function and pass it to the adapter during instantiation.
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const myService: MyService = app.get('MyService');
app.useWebSocketAdapter(new MyAdapter(app, myService));
await app.listen(3000);
}
bootstrap();
export class MyAdapter extends IoAdapter {
constructor(app: any, private readonly myService: MyService) {
super(app);
}
createIOServer(port: number, options?: any): any {
const server = super.createIOServer(port, options);
// this.myService
return server;
}
}
Edit: Had to update, you should/need to pass the app to the adapter, otherwise you overwrite the default settings which introduces a couple of problems - or at least it did for me
Most helpful comment
@underfisk you could get the service in the bootstrap function and pass it to the adapter during instantiation.
Bootstrap
Adapter
Edit: Had to update, you should/need to pass the app to the adapter, otherwise you overwrite the default settings which introduces a couple of problems - or at least it did for me