Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about making configService available to RedisIoAdapter? #2785

Closed
db2crush opened this issue Aug 21, 2019 · 3 comments
Closed

How about making configService available to RedisIoAdapter? #2785

db2crush opened this issue Aug 21, 2019 · 3 comments

Comments

@db2crush
Copy link

db2crush commented Aug 21, 2019

Feature Request

How about making configService available to RedisIoAdapter?

Is your feature request related to a problem? Please describe.

Describe the solution you'd like

import { IoAdapter } from '@nestjs/platform-socket.io';
import * as redisIoAdapter from 'socket.io-redis';
import { ConfigService } from '../config/config.service';

export class RedisIoAdapter extends IoAdapter {
**// now i am using like this line. But this is drawbacks because class is created newly.**
private configService: ConfigService = new ConfigService();

createIOServer(port: number, options?: any): any {
const server = super.createIOServer(port, options);
const redisAdapter = redisIoAdapter({
host: this.configService.getAsString(REDIS_HOST),
port: this.configService.getAsNumber(REDIS_PORT),
});
server.adapter(redisAdapter);
return server;
}
}

Teachability, Documentation, Adoption, Migration Strategy

How about inject adapter to module ???

What is the motivation / use case for changing the behavior?

@BrunnerLivio
Copy link
Member

At some point you will instantiate the RediosIoAdapter class in your main.ts file.
You can simply get the ConfigService from the application context and and pass it over to the newly created RedisIoAdapter instance:

main.ts

const app = await NestFactory.create(ApplicationModule);
const configService = app.get(ConfigService)
app.useWebSocketAdapter(new RedisIoAdapter(app, configService));

redios-io.adapter.ts

export class RedisIoAdapter extends IoAdapter {
  constructor(app, private readonly configService: ConfigService) {
    // Or simply get the ConfigService in here, since you have the `app` instance anyway
    super(app);
  } 
  ...
}

You can read more about Application Context here.
Please let me know whether you found a solution using this approach. :)

@BrunnerLivio BrunnerLivio added type: question 🙌 and removed needs triage This issue has not been looked into type: enhancement 🐺 labels Aug 22, 2019
@db2crush
Copy link
Author

Missed the way of application context.
Nice approach. thx :)

@lock
Copy link

lock bot commented Nov 20, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants