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

Custom plugins #210

Open
raulmartinezr opened this issue Mar 2, 2020 · 2 comments
Open

Custom plugins #210

raulmartinezr opened this issue Mar 2, 2020 · 2 comments

Comments

@raulmartinezr
Copy link

Hi,

Thats not an issue, but a question.

Is there some way to create custom plugins in the broker?

BR,
Raul

@romancardenas
Copy link
Contributor

Yes, it is possible, though is a little bit "difficult".

If you check the setup.py file, you will find that it defines different entry points that specify the available plugins. You can add a new plugin (it must inherit the corresponding plugin base class), add it to the setup.py file and build the project again.

It would be very nice if an abstract factory design pattern was followed when loading the plugins, making possible to pass additional plugins with no need of modifying the setup.py file.

@ShenTengTu
Copy link

ShenTengTu commented Apr 20, 2020

How to make custom plugins

For example, you want to make plugins for MQTT Borker, follow the steps as bellow:

Step 1. Add entry point in setup.py for custom plugins.

Method 1 : Use default namespace ("hbmqtt.broker.plugins"). It would load custom plugins when you create Broker instance and not specify plugin_namespace argument.

If you use the default namespace, it will load the default plugins and custom plugins.
Currently, it cannot filter the plugins you are actually using via the configuration file.

from setuptools import setup

setup(
    entry_points={
        "hbmqtt.broker.plugins": [
            "plugin_name = your_lib.module:PluginClass"
        ],
    },
)

Method 2 : Use custom namespace. In this case, you have to specify plugin_namespace argument when you create Broker instance, or it would not load your plugin.

Currently, it can't load default plugins with your plugin together if using custom namespace.

from setuptools import setup

setup(
    entry_points={
        "hbmqtt_your_lib.broker.plugins": [
            "plugin_name = your_lib.module:PluginClass"
        ],
    },
)

Step 2. Create plugin class

In your_lib/module.py:

import asyncio

class PluginClass:
    def __init__(self, context):
        self.context = context # BrokerContext
        self.config = self.context.config["custom_plugin_config"]

    @asyncio.coroutine
    def on_broker_post_start(self, *args, **kwargs):
        pass

    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants