Show / Hide Table of Contents

    MQTT Events

    Some lifetime events are fired by the MqttBroker and can be handled using the following callbacks:

    • IMqttClientConnectedCallback
    • IMqttClientDisconnectingCallback

    Example

    In the following example a message is sent as soon as the client is connected.

    • Startup
    • ConnectionCallbackHandler
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options
                    .AddMqtt())
                .AddSingletonBrokerCallbacksHandler<ConnectionCallbackHandler>();
        }
    }
    
    public class ConnectionCallbackHandler
        : IMqttClientConnectedCallback
    {
        private readonly IPublisher _publisher;
        
        public ConnectionCallbackHandler(IPublisher publisher)
        {
            _publisher = publisher;
        }
                
        public Task OnClientConnectedAsync(MqttClientConfig config) =>
            _publisher.PublishAsync(new ClientConnectedMessage());
    }
    

    See also

    Broker Callbacks

    • Improve this doc
    GitHub E-Mail
    ↑ Back to top © 2020 Sergio Aquilini