Team XSockets.NET

MQTT - MqttBridge

As most things in XSockets the MqttBridge is a module/plugin. The default module can be overwritten by you own implementation. By creating you own implementation you can bridge messages between MQTT clients and XSockets controllers. You will also be able to intercept events for the MQTT clients, such as subscribe/unsubscribe connect/disconnect etc.

Note that you do not have to implement a custom bridge to be able to send messages to MQTT clients from XSockets. Take a look at the Communication Extensions section to see how to do that

MqttBridge Methods

The default bridge has several methods and we will walk through them below. You can override all of these methods in your custom MqttBridge.

/// <summary>
/// Will fire when a new publish message is received from a MQTT client.
/// If you return false this message will not be published to MQTT clients.
/// 
/// Use this method to send messages to non-MQTT clients
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
bool OnPublish(MqttClient client, MqttMsgPublishEventArgs message);

/// <summary>
/// Will fire when a new subscription request is received.
/// Remove topics not allowed from the MqttMsgSubscribeEventArgs to deny subscriptions.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
void OnSubscribe(MqttClient client, MqttMsgSubscribeEventArgs message);

/// <summary>
/// Will fire when a MQTT client unsubscribes
/// </summary>
/// <param name="client"></param>
/// <param name="message"></param>
void OnUnsubscribe(MqttClient client, MqttMsgUnsubscribeEventArgs message);

/// <summary>
/// Will fire when a MQTT client disconnects
/// </summary>
/// <param name="client"></param>
void OnClientDisconnect(MqttClient client);

/// <summary>
/// Will fire when a client tries to connect, returning false will reject the client connection.
/// If you authenticated the connection in the XSockets Authenticaiton Pipeline you can access it via
/// client.ConnectionContext
/// </summary>
/// <param name="message"></param>
/// <param name="client"></param>
/// <returns></returns>
bool VerifyConnection(MqttMsgConnect message, MqttClient client);

/// <summary>
/// Will fire when a MQTT client connects
/// </summary>
/// <param name="client"></param>
void OnClientConnect(MqttClient client, MqttMsgConnectEventArgs message);

/// <summary>
/// For publishing a message from XSockets to MQTT clients.
/// 
/// Normally you should use the extensions for sending binary or text data,
/// but if you built a MqttMsgPublish object you may call this directly
/// </summary>
/// <param name="message"></param>
void PublishToMqttClients(MqttMsgPublish message);

Bridge Message from MQTT to XSockets

So, by creating a custom bridge and override the OnPublish method we can send MQTT messages into XSockets.

public class MyMqttBride : MqttBridge
{
    //We just use the generic controller, but we can use any controller
    private IXSocketController generic;
    public MyMqttBride()
    {
        generic = new Generic();
    }
    public override bool OnPublish(MqttClient client, MqttMsgPublishEventArgs message)
    {
        //Extract the message (we assume that it is JSON being sent in this sample)
        var json = System.Text.Encoding.UTF8.GetString(message.Message);
        //Create an IMessage object
        var m = new Message(json, message.Topic);
        //Send the message to XSockets clients on a specific controller (foo).
        generic.InvokeToAll<Foo>(m);

        // Allow the message to be published to MQTT clients
        return true;
    }
}

Result

We connect the MQTT.fx client and publish a message. Since we have our custom bridge (above) the OnPublish method will send the MQTT message to XSockets clients connected to the Foo controller.

Sending topic test and payload {"name":"steve", "age":43} from the MQTT client. The custom MqttBridge sends the message to all XSockets clients on the Foo controller

mqtt custom bridge

results matching ""

    No results matching ""