Yakifo/amqtt

Making use of events

hollymcr opened this issue · 2 comments

I can't see any documentation on this, so:

I can see that there is an event system which means that I should be able to add code to a broker to respond to things like client connections, etc. But I can't see how to do that; given it's within the plugin system it suggests I would need to write my own plugin?

If it's simple to do, could the broker_start.py sample be modified to include some event callbacks? Or if it's too complex for that simple example can someone point me in the right direction?

That is not really a documented feature but you can register for transition changes. Checkout https://github.com/Yakifo/amqtt/blob/master/amqtt/session.py#L148-L163 to see the available transitions of the client session that you can hook into. The broker transitions are to be found here: https://github.com/Yakifo/amqtt/blob/master/amqtt/broker.py#L208-L228

To register a method named some_callback to be invoked e.g. when a connection is established:

        self.client.session.transitions.on_enter_connected(
             some_callback)

Interesting, thanks you and I'll take a look as it might have some uses. But it doesn't seem to give me what I need.

Suppose I wanted something like this:

async def on_client_connect(*args, **kwargs):
    print(args, kwargs)

broker = Broker({...})
broker.on_broker_client_connected = on_client_connect

The "on_broker_client_connected" event exists but only seems to be sent to plugins (from my reading of the code). I can't see how to create or load my own plugin to take advantage of this, though (and ideally a plugin shouldn't be needed anyway). I looked at whether I could add a generic plugin within the amqtt code that could act as a way for code using amqtt to associate custom code with any event, but either my Python or amqtt internals knowledge let me down!