Copied!
Private Cloud
  • Application Development Services
    • Quick Start
    • API Gateway
      • API Request
        • Request Structure
        • Authorization Method
        • Sign Requests
        • Global Error Codes
      • Best Practice
        • Set Postman Environment and Call API Operations
      • API List
        • Basic API
          • Authorization Token Management
            • Refresh Token
            • Get a Token
        • IoT Core
          • Device Management
            • Query Devices in Space
            • Get Status Reporting Log
            • Get Update Progress
            • Query Device Details in Bulk
            • Delete Device
            • Get Update Information
            • Query Device Details
            • Confirm Start of Update
            • Get Device State
            • Get Operation Log
            • Modify Basic Properties
            • Freeze/Unfreeze Device
            • Transfer Device
            • Restore Factory Defaults
            • Query Pairing Result
          • Device Control
            • Send Property
            • Query Properties
            • Modify Properties
            • Set Desired Property
            • Query Desired Properties
            • Query Things Data Model
            • Device Control (Standard Instruction Set)
              • Get Status Set
              • Send Commands
              • Get Specifications and Properties of the Device
              • Get Instruction Set of the Device
              • Get Instruction Set of the Category
              • Get Status of a Single Device
              • Get Latest Status of Multiple Devices
          • Device Group
            • Create Group
            • Delete Group
            • Query Group
            • Rename Group
            • Get Group in Space
            • Add Device
            • Delete Devices
            • Get Device Group
            • Query Group Properties
            • Send Group Properties
            • Query Group Devices
    • Message Gateway
      • Preparations
      • Development Guide
        • Integrate with Message Service
        • Data Signature
        • Message Types
      • Best Practices
        • Get Push Messages by Pulsar (Java SDK)
        • Get Push Messages by Pulsar (Python SDK)
        • Get Push Messages by Pulsar (Go SDK)
        • Get Push Messages by Pulsar (Node.js SDK)
        • Get Push Messages by Pulsar (C# SDK)
  • App Development
    • Preparation
    • Developer Guide for iOS
      • Fast Integration with Cube App SDK for iOS
      • Upgrade Guide
      • Quick Start with Cube App SDK for iOS
      • iOS App: Implement User Account Features
      • User Account Management (iOS)
        • Register and Login with Phone Number
        • Register and Login with Email Address
        • Login with UID
        • Login with Third-Party Account
        • Register and Login with Anonymous Account
        • QR Code-Based Login Authorization
        • Manage User Accounts
      • Home Management
        • Home Information Management
        • Room Information Management
        • Member Information Management
      • Device Pairing (iOS)
        • Wi-Fi EZ Mode
        • AP Mode
        • Wired Mode
        • Sub-Device Pairing
        • NB-IoT Device Pairing
        • HomeKit Device Pairing
        • Pegasus Pairing
        • CPPLUS Link-Based Device Binding
        • Matter Devices
          • Prepare for Integration with Matter Device
          • Pair Matter Device
        • Pair with QR Code on Device
      • Bluetooth Devices
        • Bluetooth Mesh
        • CPPLUS Mesh
      • Device Management
        • Firmware Update
        • Query Reported DP Logs
        • Device Sharing
        • Backup Wi-Fi Networks
        • Offline Device Alerts
        • CPPLUS Link-Based Devices
        • Manage Matter Devices
      • Device Control
        • Multi-Control Linkage
        • Scheduled Tasks
        • Group Management
        • All Category Control Panel
        • Air Conditioning Category Control Panel
        • Switch Control Panel
        • Device DP Parser
      • Smart Scenes
        • Scene Recommendation
      • Common API Methods
      • Integrate with Push Notifications
      • Message Management
        • Configure Push Notifications
        • Message Capabilities
      • Configure Widget Project
      • Error Codes
      • FAQ
      • Extension SDK
        • IPC SDK
          • SDK Architecture
          • Fast Integration with IPC SDK for iOS
          • Quick Start with IPC SDK for iOS
          • IPCs
          • Device Pairing
          • IPC Control (iOS)
          • Live Video Streaming
          • Playback of Video Stored on SD Card
          • SD Card Management
          • Audio and Video Features
          • Low Power Doorbell
          • PTZ Control
          • Doorbell Call Service
          • Cloud Storage
          • Cloud Storage Debugging Tool
          • Detection Alerts
          • Encrypted Images
          • Timeline View
          • Offline Log
          • Migrate IPC UI BizBundles
          • P2P Capabilities
          • IPC SDK Automated Test Tool
          • IPC SDK Test Cases (Reference)
          • FAQ
          • Error Codes
        • Sweeper SDK
          • P2P Download from Robot Vacuums
          • Gyroscope and Visual Robot Vacuums
          • Laser Robot Vacuum
          • Voice Package Download
          • Error Codes
          • Sample Code
        • Smart Lock SDK
          • Wi-Fi Lock
          • Bluetooth Lock
          • Wi-Fi Lock Audio and Video Capability
          • Smart Lock Basic API
      • What is UI BizBundle SDK for iOS?
        • Preparation
        • Integrate with Framework
        • Home Management UI BizBundle
        • Device Pairing UI BizBundle
        • Scene UI BizBundle
        • Multi-Control Linkage UI BizBundle
        • Device Control UI BizBundle
        • OTA UI BizBundle
        • Group Management UI BizBundle
        • Device Details UI BizBundle
        • Panel Multilingual BizBundle
        • IPC UI BizBundles
        • Cloud Storage UI BizBundle
        • FAQ UI BizBundle
        • Message Center UI BizBundle
        • Voice Skills Account Linking UI BizBundle
        • Theme Color Configuration BizBundle
        • Sharing UI BizBundle
      • Changelog
    • Developer Guide for Android
      • Fast Integration with Cube App SDK for Android
      • Upgrade Guide
      • User Account Management
        • Register and Login with Phone Number
        • Register and Login with Email Address
        • Login with UID
        • Login with Third-Party Account
        • Register and Login with Anonymous Account
        • QR Code-Based Login Authorization
        • Manage User Accounts
      • Home Management
        • Home Information Management
        • Room Information Management
        • Member Information Management
      • Device Pairing (iOS)
        • Wi-Fi EZ Mode
        • AP Mode
        • Wired Mode
        • Pair with QR Code on Device
        • Pegasus Pairing
        • Sub-Device Pairing
        • NB-IoT Device Pairing
        • CPPLUS Link-Based Device Binding
        • Matter Devices
          • Prepare for Integration with Matter Device
          • Pair Matter Device
        • Pair with QR Code on Device
      • Bluetooth Devices
        • Bluetooth LE Devices
        • Bluetooth Mesh
        • CPPLUS Mesh
      • Device Management
        • Firmware Update
        • Query Reported DP Logs
        • Device Sharing
        • Backup Wi-Fi Networks
        • Notifications of Devices Getting Offline
        • CPPLUS Link-Based Devices
        • Manage Matter Devices
      • Device Control
        • Sub-Device Control Through Zigbee Gateway
        • Multi-Control Linkage
        • Scheduled Tasks
        • Group Management
        • Device DP Parser
      • Smart Scenes
        • Scene Recommendation
      • Common API Methods
      • Integrate with Push Notifications
        • Integrate with FCM Push
        • Integrate with Xiaomi Push Notifications
        • Integrate with Huawei Push Notifications
      • Message Management
        • Configure Push Notifications
        • Message Capabilities
      • Error Codes
      • FAQ
      • Extension SDK
        • IPC SDK
          • IPC SDK Architecture
          • Fast Integration with IPC SDK for Android
          • IPCs
          • Device Pairing
          • Live Video Streaming
          • SD Card-Stored Video Playback
          • SD Card Management
          • Audio and Video Features
          • Cloud Storage
          • Cloud Storage Debugging Tool
          • Alert List
          • Video Alerts
          • Low Power Doorbell
          • Encrypted Images
          • Timeline View
          • P2P Capabilities
          • IPC SDK Automated Test Tool
          • IPC SDK Test Cases (Reference)
          • Error Codes
          • SDK Log
          • Changelog
        • Sweeper SDK
          • P2P Download from Robot Vacuums
          • Gyroscope and Visual Robot Vacuums
          • Laser Robot Vacuum
          • Voice Package Download
          • Changelog
          • Error Codes
          • Sample Code
        • Smart Lock SDK
          • Wi-Fi Smart Lock
          • Bluetooth Smart Lock
          • Wi-Fi Smart Lock Audio and Video Capabilities
          • Zigbee Lock
          • Smart Lock Basic API
          • Changelog
      • What is UI BizBundle SDK for iOS?
        • Preparation
        • Integrate with Framework
        • Home Management UI BizBundle
        • Device Pairing UI BizBundle
        • Generic Pairing BizBundle
        • Device Control UI BizBundle
        • Multi-Control Linkage UI BizBundle
        • Scene UI BizBundle
        • Group Management UI BizBundle
        • Device Details UI BizBundle
        • OTA UI BizBundle
        • IPC UI BizBundles
        • Cloud Storage UI BizBundle
        • Message Center UI BizBundle
        • FAQ UI BizBundle
        • Maps Service UI BizBundle
        • Voice Skills Account Linking UI BizBundle
        • Panel Multilingual BizBundle
        • Theme Color Configuration BizBundle
        • Sharing UI BizBundle
        • FAQ
      • Changelog
  1. Home
  2. Application Development Services
  3. Message Gateway
  4. Get Push Messages By Pulsar Nodejs SDK

Get Push Messages by Pulsar (Node.js SDK)

Last Updated on: 2024-09-10 07:19:13

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.

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 the demo source code

Click Pulsar SDK 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 a 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 on the Tuya IoT Development Platform.

  • accessKey: Enter the value of Access Secret in the Authorization Key section on the Tuya IoT Development Platform.

  • url: For more information about the parameters, see src/config.ts. Be sure to use the URL of the specified domain name where the private cloud is deployed.

  • env: the message subscription channel.

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

    Note

    TEST in env represents the test environment and PROD represents the production 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. The messages reported by the test device will be sent through the test channel to accelerate debugging of 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 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. Display the data. For more information about field descriptions, see Message Types.

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'
    }
    Important

    For more information about the payload data parsing method, see Data Signature. 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 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'
    }
    
  • Prev Doc Get Push Messages by Pulsar (Go SDK)
  • Next Doc Get Push Messages by Pulsar (C# SDK)
This page contains content