Push Agent as a subscriber

By default, Push Agent can send data to the cloud. You can edit the Push Agent code to receive data from the cloud.

Data reception function

The function in the following example is called each time a message is received. The function processes the received message according to the logic defined in the function and then updates the messageVariable variable value.

private Task SubscribeClientMqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs e)
{
    var message = e.ApplicationMessage.ConvertPayloadToString();
    var messageVariable = Project.Current.GetVariable("Model/Message");
    messageVariable.Value = Newtonsoft.Json.Linq.JObject.Parse(message)["Rows"][0]["Variables"][0]["Value"].ToString();
    return Task.CompletedTask;
}

Subscriber configuration parameter

The following example shows the data reception function that is passed as a configuration parameter of the Subscriber. In addition to the base configuration of the Publisher, the subscriber method is configured in the Start() method.

public override void Start()
{
    // PushAgent Default configuration

    // Add subscriber
    mqttClientConnector.AddSubscriberAsync("my_custom_subscriber_topic", 1, SubscribeClientMqttMsgPublishReceived);
}



public async void AddSubscriberAsync(string topic, int qosLevel, Func<MqttApplicationMessageReceivedEventArgs, Task> subscribeClientMqttMsgPublishReceived)
{
    mqttClient.ApplicationMessageReceivedAsync += subscribeClientMqttMsgPublishReceived;
    await mqttClient.SubscribeAsync(topic, GetQoSLevel(qosLevel)).ConfigureAwait(false);
}