Team XSockets.NET

MQTT - Communication Extensions

This section covers how to send (publish) messages to MQTT clients from XSockets controllers.

Almost all methods for communication in XSockets are extension methods for the IXSocketController interface. Some of them are frequently used in this book, such as InvokeTo... PublishTo... etc.

To be able to send messages from XSockets controller to MQTT clients we built a few extra extensions that are included in the XSockets.MQTT package.

The extension for communicating with MQTT clients is under the namespace XSockets.Protocol.Mqtt.Extensions

public class Foo : XSocketController
{
    public async Task Bar()
    {        
        //Do some stuff

        //Publish to MQTT clients
        this.MqttPublish("baz",new { Name="Steve", Age=42 });

        //Do some other stuff
    }
}

Example

Below we have started the XSockets server with MQTT enabled. We also connected with the MQTT.fx client and subscribed to the topic baz.

mqttfx subscribe baz

Now we connect to XSockets from Putty and call the bar method on the foo controller (declared above). That method will then use the extension/helper to send a message with topic baz to the MQTT clients (subscribers).

mqttfx putty publish

Summary

A basic sample on how to publish messages to MQTT clients from XSockets. Do note that you can send byte[] as well since the MqttPublish extension has a overload.

results matching ""

    No results matching ""