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

Feat: onApplicationReady Hook #13806

Open
1 task done
StimulCross opened this issue Jul 20, 2024 · 8 comments
Open
1 task done

Feat: onApplicationReady Hook #13806

StimulCross opened this issue Jul 20, 2024 · 8 comments

Comments

@StimulCross
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

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

There are a lot of potential tasks that can be done successfully only after the start of an application (when the app is ready to handle connections).

The onApplicationBootstrap triggers before await app.listen(), so it can't be used for such tasks (correct me if I'm wrong).

Describe the solution you'd like

Although this can be easily implemented using events, another good option would be introducing a new hook that triggers after await app.listen().

Teachability, documentation, adoption, migration strategy

The hook's name can be onApplicationReady or onApplicationStart. The usage of the hook will be pretty similar to the other hooks:

export class CustomProvider implements OnApplicationReady {
    onApplicationReady(): void {
        console.log('Ready!')
    }
}

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

One of the use cases could be webhook subscriptions. When I subscribe to webhooks, the publisher sends me a POST request to verify my webhook callback. It is safer to perform this task after the application is ready to listen for connections.

@StimulCross StimulCross added needs triage This issue has not been looked into type: enhancement 🐺 labels Jul 20, 2024
@CarltonK
Copy link

CarltonK commented Sep 3, 2024

+1 support
Is this feature being considered by the NestJs team?

@micalevisk
Copy link
Member

micalevisk commented Sep 3, 2024

I'm not sure about the naming and the role of that hook. I'd need to think more about it. But I can say that maybe we need a more specific name because 'ready' can be interpreted in several ways

You can supply a callback already to app.listen method but I get that you want to make things more coupled with nestjs providers and modules; more high-level

@CarltonK
Copy link

CarltonK commented Sep 3, 2024

I'm not sure about the naming and the role of that hook. I'd need to think more about it. But I can say that maybe we need a more specific name because 'ready' can be interpreted in several ways

You can supply a callback already to app.listen method but I get that you want to make things more coupled with nestjs providers and modules; more high-level

@micalevisk Maybe something like onApplicationInit

@micalevisk
Copy link
Member

'init' would be confusing because we have app.init()

@kamilmysliwiec
Copy link
Member

What about using https://docs.nestjs.com/techniques/events and then

await app.listen(...);
await app.get(EventEmitter2).emit(...);

Done(?) This gives you full control too

@StimulCross
Copy link
Author

@kamilmysliwiec I personally already use this approach. It just feels like this functionality should be available out of the box with the rest of the hooks and have similar usage. Without this hook, lifecycle hooks feel incomplete.

@kamilmysliwiec
Copy link
Member

The thing is, onApplicationReady is just somewhat vague as "ready" might mean different things depending on the context. For some, it's just that the app is already listening to HTTP requests, but for those who have hybrid applications (HTTP + message broker) the definition is probably going to be different. Not to mention that those who use the "Standalone application" mode wouldn't use that hook at all.

@StimulCross
Copy link
Author

Yeah, I didn't take those things into account when I came up with the hook name. Naming is not my strong point :) There's a lot to think about here. You guys know better anyway.

@micalevisk micalevisk added needs clarification and removed needs triage This issue has not been looked into labels Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants