Get Push Messages by Pulsar (C# SDK)

Last Updated on : 2025-02-07 02:24:11download

A cloud project provides the message subscription feature. If the status of a device in the project changes, such as registration, data reporting, and offline events, Message Service is used to actively push event data to external partners via Pulsar. This helps to implement real-time and persistent messages.

Prerequisites

  • You have created a project. For more information about project creation, see Quick Start.
  • Message Service is enabled and the device is linked with the project. For more information, see Manage Message Service and Link Devices.
  • You have installed the C# development tool Rider.
  • You have installed the .NET IDE.

Procedure

Currently, Tuya Smart Message Service is customized and modified based on open source Pulsar. The Pulsar SDK for C# is provided.

In this topic, the C# development tool Rider is used to receive messages.

Step 1: Clone the Github source code and import the project

Clone the Github source code: Github

Get Push Messages by Pulsar (C# SDK)

Step 2: Configure project parameters

Modify the configuration information in the sample code Program.cs:

  • ACCESS_ID: Enter the value of Access ID/Client ID in the Authorization Key section on the Tuya Developer Platform.
  • ACCESS_KEY: Enter the value of Access Secret/Client Secret in the Authorization Key section on the Tuya Developer Platform.
  • PULSAR_SERVER_URL: Select based on the data center where you have subscribed to the message service.
    • China Data Center: pulsar+ssl://mqe.tuyacn.com:7285/
    • Western America Data Center: pulsar+ssl://mqe.tuyaus.com:7285/
    • Central Europe Data Center: pulsar+ssl://mqe.tuyaeu.com:7285/
    • India Data Center: pulsar+ssl://mqe.tuyain.com:7285/
  • MQ_ENV: the message subscription channel.
    • MQ_ENV_PROD represents the production environment.
    • MQ_ENV_TEST represents the test environment.

Step 3: Operate a device on the app and receive messages in the test environment

  1. On the development tool Rider, right-click the tuya-pulsar-sdk-dotnet project and select Run.

    Get Push Messages by Pulsar (C# SDK)

  2. Operate the device on the mobile app. For example, turn on a device, or adjust other parameters of the device.

  3. Automatically get the push messages on Rider.

    Get Push Messages by Pulsar (C# SDK)

  4. Display the data. For more information about field descriptions, see Message Types.

Parse push messages

Turn on the device

  • Received message

    payload={"data":"uSqWibG1DRsqYj/EWW5xRP4NlAo948wkhatFSvvtl3lgYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXPxk8qvcNe3J+kdU32jkbjxlzxxG+g6m1fHPH7m4+OHuh8Q09f8Ff1CNONQFvMLfStqmh6zyXimOxTy0x9pIPxUPxHwhUndn4xepP09qbODn0","protocol":4,"pv":"2.0","sign":"16cc5405b1e843bdd6f25063fa8ce330","t":1631158677498}
    payloadJson={
      "data": "uSqWibG1DRsqYj/EWW5xRP4NlAo948wkhatFSvvtl3lgYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXPxk8qvcNe3J+kdU32jkbjxlzxxG+g6m1fHPH7m4+OHuh8Q09f8Ff1CNONQFvMLfStqmh6zyXimOxTy0x9pIPxUPxHwhUndn4xepP09qbODn0",
      "protocol": 4,
      "pv": "2.0",
      "sign": "16cc5405b1e843bdd6f25063fa8ce330",
      "t": 1631158677498
    }
    
  • Parse payload data

    data={"dataId":"52effb10-111f-11ec-935e-02426139acc4","devId":"vde*******70149816","productKey":"AAi******Tgmo64Hq","status":[{"1":"true","code":"switch_led","t":1631158677476,"value":true}]}
    

    For more information about the payload data parsing method, see Data Signature and Decryption. The payload decryption method has been encapsulated in the SDK.

Adjust device parameters

  • Received message

    payload={"data":"vJuyzlQPkXrZn/9OSwTOP5bmlNruRVFOZoc1Zod3Ul9gYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXP4F82++oke4J6O8vAvLCpDxsOTqBLnuQXj9p+sknAK6Z1NAGbES2LQ4fDPlg/ZFqoccIX3s9PhIo9cPZSzctc9MrqoBg1e7jFRfIAiY3bPYx","protocol":4,"pv":"2.0","sign":"53b8014e0c2d445e8dca665a02236100","t":1631158740025}
    payloadJson={
      "data": "vJuyzlQPkXrZn/9OSwTOP5bmlNruRVFOZoc1Zod3Ul9gYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXP4F82++oke4J6O8vAvLCpDxsOTqBLnuQXj9p+sknAK6Z1NaAGbES2LQ4fDPlg/ZFqoccIX3s9PhIo9cPZSzctc9MrqoBg1e7jFRfIAiY3bPYx",
      "protocol": 4,
      "pv": "2.0",
      "sign": "53b8014e0c2d445e8dca665a02236100",
      "t": 1631158740025
    }
    
  • Parse the data

    data={"dataId":"782ee45f-111f-11ec-935e-02426139acc4","devId":"vde*******70149816","productKey":"AAi******Tgmo64Hq","status":[{"3":"177","code":"bright_value","t":1631158739964,"value":177}]}