Get Push Messages by Pulsar (Node.js SDK)

Last Updated on : 2024-06-20 06:27:28download

A cloud project provides the message subscription feature. If the device status in the project changes, such as registration, data reporting, and offline events, Message Service is used to actively push event data to external partners with 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. For more information, see Message Service.
  • You have set up the development environment for Node.js.

Procedure

Currently, Tuya Smart Message Service is customized and modified based on open source Pulsar. The Pulsar SDK for Node.js is provided.
In this topic, the Node.js development tool WebStorm is used to receive messages.

Step 1: Download demo source code

Click Pulsar SDK for Node.js to download the package to the local computer and decompress the package.

Step 2: Set up the test environment

  1. Use WebStorm to open the source code project: Click Open to find the downloaded and decompressed file in the previous step.

    Get Push Messages by Pulsar (Node.js SDK)

  2. Add dependency environment.

    • crypto-js: 4.0.0 or later

    • ws: 7.4.5 or later

      Get Push Messages by Pulsar (Node.js SDK)

Step 3: Configure project parameters

Modify the parameters in the example/index.js file. This topic takes the test channel as an example.

const client = new TuyaWebsocket({
  accessId: "xt*****ff**n1****8ufo",
  accessKey: "479bcb7345******582d9c******4ef7",
  url: TuyaWebsocket.URL.CN,
  env: TuyaWebsocket.env.TEST, // Test channel
  maxRetryTimes: 50,
});

The parameters are described as follows.

  • accessId: Enter the value of Access ID in the Authorization Key section.

  • accessKey: Enter the value of Access Secret in the Authorization Key section.

  • url: Select an option for the area where the API operation is called. For more information about the parameters, see src/config.ts.

    • TuyaWebsocket.URL.CN (China): wss://mqe.tuyacn.com:8285/
    • TuyaWebsocket.URL.US (Western America): wss://mqe.tuyaus.com:8285/
    • TuyaWebsocket.URL.EU (Central Europe): wss://mqe.tuyaeu.com:8285/
    • TuyaWebsocket.URL.IN (India): wss://mqe.tuyain.com:8285/
  • env: message subscription channel.

    • TuyaWebsocket.env.PROD represents the formal environment.
    • TuyaWebsocket.env.TEST represents the test environment.
  • maxRetryTimes: the maximum number of reconnections.

    Note: TEST in env represents the test environment and PROD represents the formal environment.

    Get Push Messages by Pulsar (Node.js SDK)

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

  1. Configure a test device.

    1. On the page of Message Service, after your subscription is ready, Subscribed appears in the Status column. Click the Message Test tab.
      Get Push Messages by Pulsar (Node.js SDK)

    2. Enter the associated device ID in the search box, click the magnifier icon, and then double-click the selected device.

      Get Push Messages by Pulsar (Node.js SDK)

    3. If an associated device is set as a test device, the message reported by it will be sent to the test channel for you to debug device functions.

  2. On the development tool WebStorm, open the Terminal and run the yarn code.

    1. yarn

    2. yarn example

    3. Execution result:

      even@MacBook-Air message-subscription-websocket-master % yarn
      yarn install v1.22.11
      [1/4] 🔍  Resolving packages...
      success Already up-to-date.
      $ husky install
      husky - not a Git repository, skipping hooks installation
      ✨  Done in 1.32s.
      even@MacBook-Air message-subscription-websocket-master % yarn example
      yarn run v1.22.11
      $ npm run build && node ./example/index.js
      npm WARN lifecycle The node binary used for scripts is /var/folders/jf/_0x1y81n67x979xf09m_cv580000gn/T/yarn--1630917003536-0.5926954827118307/node but npm is using /Users/even/.nvm/versions/node/v14.17.5/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
      
      > message-subscription-websocket@0.0.1 build /Users/even/Documents/SDK/Nodejs/message-subscription-websocket-master
      > tsc
      
      open
      

      Get Push Messages by Pulsar (Node.js SDK)

  3. Control the device with a mobile app.

    1. Turn on the switch of the test light.
    2. Adjust the brightness of the light.
  4. Automatically get the push messages on WebStorm.
    Get Push Messages by Pulsar (Node.js SDK)

  5. For more information about field descriptions, see Data format description and usage notice.

Parse push messages

Turn on the light

  • Received message

    INFO 1630917829404  receive msg, jsonMessage={"messageId":"CLe1/w4QACAA","payload":"eyJkYXRhIjoidUdtTE1YempLczJpT242eWJ6WTBIUk9yemlzYlpiVEJBakIxZnVTcHQzcmF3SXEybUcvZlpUZDlUQ2ltcXBHUHpRdGY5d0Zoa1NRNklCRTJQM3lZUGo2Q3JzcUZFUXZMOVBGZUhwRDlaOUthZHpuRndTUVg2c2dSYTJmMkFORkhEVEM4cFowK2ZMbldnZTdBcVBHNExkeG9iT1BDN2tOcGs5MmhtWGpCMFBFT1oyRHU0TWJQNE5TTjQ5UWNKNE1QRTJsU2hlQ01yZWw5QjE3VFQ4QTQwMFRsdjh5MG5sMDRaTXE1YStpbUxDdmpHSDV0YWxscEkxMFJaeG9mSjcyZiIsInByb3RvY29sIjo0LCJwdiI6IjIuMCIsInNpZ24iOiI5ZjRiNTY1YjY4YjA0ODk2ZWZhODE0MDhkOTQxYzY4NyIsInQiOjE2MzA5MTc1NzYwNDh9","properties":{},"publishTime":"2021-09-06T16:39:36.048+08:00","redeliveryCount":0,"key":"vdevo*******286991"}
    
  • Parse payload data

    the real message data: {
    payload: {
       data: {
          dataId: 'f73709fe-0eed-11ec-8511-024283c73485',
          devId: 'vdevo*******286991',
          productKey: 'na****z',
          status: [Array]
       },
       protocol: 4,
       pv: '2.0',
       sign: '9f4b565b68b04896efa81408d941c687',
       t: 1630917576048
    },
    messageId: 'CLe1/w4QACAA',
    properties: {},
    publishTime: '2021-09-06T16:39:36.048+08:00',
    redeliveryCount: 0,
    key: 'vdevo*******286991'
    }
    

    Note: For more information about the data parsing method, see Data signing. The payload decryption method has been encapsulated in the SDK. For more information, see src/utils.ts.

Adjust the brightness of the light

  • Received message

    INFO 1630917829686  receive msg, jsonMessage={"messageId":"CLe1/w4QAyAA","payload":"eyJkYXRhIjoidEdHZUw4b0NYdXpZdlVnNytUKzlzVmZXUVowQXJuWVM0RlExd2R2a01oTGF3SXEybUcvZlpUZDlUQ2ltcXBHUHpRdGY5d0Zoa1NRNklCRTJQM3lZUGo2Q3JzcUZFUXZMOVBGZUhwRDlaOUthZHpuRndTUVg2c2dSYTJmMkFORkhEVEM4cFowK2ZMbldnZTdBcVBHNExTbFY0VkV2N050WjBUOXdzTTB4c1FlK0dkM2xjRFJqdG9Xa2VIVGFJaHZOUGluNDRBbTI2bTV1V0Z1L2R6a0xUd3hFNTlWdmg5S2tJOUpmTjR3ZTlXUzZycXVOWVJYcTNJN0hZRVBPeGY3OTNpajc5TUZHZUFEMWJLNHo1aWxHeG9vUlRsQjNNdVVFdXdWUmJOWlRJZnZ2YmNjWi9WUjdyOHRhQ3VuQ3QrbGJLVVl3clc1a2ZnZGxQUHlSY25HT013PT0iLCJwcm90b2NvbCI6NCwicHYiOiIyLjAiLCJzaWduIjoiYmEzZGE5NzcyNzBhOTRhOTQ3NmJhNmEzY2RlOWQ0ZTciLCJ0IjoxNjMwOTE3ODI5ODg1fQ==","properties":{},"publishTime":"2021-09-06T16:43:49.885+08:00","redeliveryCount":0,"key":"vdevo*******286991"}
    
  • Parse the data

     the real message data: {
      payload: {
        data: {
          dataId: '8e8e7a0b-0eee-11ec-8511-024283c73485',
          devId: 'vdevo*******286991',
          productKey: 'na****jz',
          status: [Array]
        },
        protocol: 4,
        pv: '2.0',
        sign: 'ba3da977270a94a9476ba6a3cde9d4e7',
        t: 1630917829885
      },
      messageId: 'CLe1/w4QAyAA',
      properties: {},
      publishTime: '2021-09-06T16:43:49.885+08:00',
      redeliveryCount: 0,
      key: 'vdevo*******286991'
    }