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

Add connections inside a plugin #2754

Closed
duro opened this issue Sep 5, 2015 · 5 comments
Closed

Add connections inside a plugin #2754

duro opened this issue Sep 5, 2015 · 5 comments
Assignees
Labels
Milestone

Comments

@duro
Copy link

@duro duro commented Sep 5, 2015

I have a plugin that gets registered via a manifest (Glue). I am wondering if it can define it's own connection via the server object it is passed? Or can connections only be created by the main Server instance, and accessed via a label in the plugin?

@hueniverse
Copy link
Contributor

@hueniverse hueniverse commented Sep 5, 2015

It's a bit delicate. It can use server.root.connection() but that connection is not going to show up within the plugin own server.connections. Handle with care.

@hueniverse hueniverse closed this Sep 5, 2015
@hueniverse hueniverse self-assigned this Sep 5, 2015
@duro
Copy link
Author

@duro duro commented Sep 5, 2015

@hueniverse: so then the recommended approach is to setup connections at the highest level of the Server. These does seem to fly in the face of Hapi's modular approach where everything is a plugin. Because now I have a parent that is defining things that the plug-ins have to assume will be present. Makes moving a plugin from one server to another require that the new server implement the plug-ins connection.

No big deal for my app, but seems like a chink in the foundation that Hapi establishes.

@hueniverse
Copy link
Contributor

@hueniverse hueniverse commented Sep 5, 2015

The contract is that server.connections doesn't change once you enter the register() plugin method. This creates the limitation that when a plugin calls server.root.connection(), that connection is not directly available to it. You could make it available to a sub-plugin of that by calling server.root.select() to get the connection you just added and then server.register() on the returned selection.

The current API protects you from unexpected changes in the environment such as a new connection showing up during the registration process. By forcing you to use server.root, it is making it clear that you are basically stepping outside of the current plugin realm.

Also keep in mind that adding a connection halfway through the registration process could mess things up for the entire server. For example, if the plugin adding the connection is being registered after some authentication plugins, those registrations will not apply to the newly created connection.

There is a lot to consider when setting up your server and this is why this API while possible, isn't a great idea.

@hueniverse hueniverse reopened this Oct 3, 2015
@hueniverse
Copy link
Contributor

@hueniverse hueniverse commented Oct 3, 2015

Now with the introduction of connectionless plugins, you have a new pattern that is safer to use. You create a plugin with attributes.connections set to false and inside called server.connection(). The connection() method will return a new server object you can use from that point on instead of the server object passed to the register() method.

@hueniverse hueniverse closed this in e157ba6 Oct 3, 2015
@hueniverse hueniverse added this to the 10.2.0 milestone Oct 3, 2015
@hueniverse hueniverse added this to the 10.3.0 milestone Oct 3, 2015
@hueniverse hueniverse removed this from the 10.2.0 milestone Oct 3, 2015
@hueniverse hueniverse added request and removed support labels Oct 3, 2015
@hueniverse hueniverse changed the title Can a plugin define it's own connection? Add connections inside a plugin Oct 4, 2015
@hueniverse hueniverse reopened this Oct 4, 2015
@hueniverse hueniverse closed this in e799439 Oct 4, 2015
@Marsup Marsup added feature and removed request labels Sep 20, 2019
@lock
Copy link

@lock lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants