Get Push Messages by Pulsar (Go SDK)

Last Updated on : 2024-06-20 06:30:38download

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.
  • You have subscribed to Message Service. For more information, see Message Service.
  • You have installed the development tool GoLand.

Procedure

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

Step 1: Set up development environment

  1. Get the SDK address at Tuya-Pulsar-Go.

    Get Push Messages by Pulsar (Go SDK)

  2. Go to Projects > Get from Version Control > URL, enter the SDK address, and then click Clone.
    Get Push Messages by Pulsar (Go SDK)

Step 2: Configure project parameters

In the example > main.go file, edit the parameters including PulsarAddr, accessID and accessKey.

Get Push Messages by Pulsar (Go SDK)

  • accessId: Enter the value of Access ID in the Authorization Key section. For more information, see Cloud project parameters.
  • accessKey: Enter the value of Access Secret in the Authorization Key section. For more information, see Cloud project parameters.
  • PulsarAddr: Select an option for the area where the API operation is called. For more information about the parameters, see client.go.
    • PulsarAddrCN(China) = “pulsar://mqe.tuyacn.com:7285”
    • PulsarAddrEU(Europe) = “pulsar://mqe.tuyaeu.com:7285”
    • PulsarAddrUS(America) = “pulsar://mqe.tuyaus.com:7285”

Example

// SetInternalLogLevel(logrus.DebugLevel)
tylog.SetGlobalLog("sdk", false)
accessID := "xtu7maff*****8ufo"
accessKey := "479bcba6d******c4e080f7"
topic := pulsar.TopicForAccessID(accessID)

// create client
cfg := pulsar.ClientConfig{
	PulsarAddr: pulsar.PulsarAddrCN,
}
c := pulsar.NewClient(cfg)

Step 3: Run the file

Right-click the file at example > main.go, and select Run ‘go build main.go’ to run the file.

Get Push Messages by Pulsar (Go SDK)

Execution result:

GOROOT=/usr/local/go #gosetup
GOPATH=/Users/even/Documengts/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/jf/_0x1y81n67x979xf09m_cv580000gn/T/GoLand/___go_build_main_go /Users/even/Documents/SDK/GoLang/tuya-pulsar-sdk-go/example/main.go #gosetup
/private/var/folders/jf/_0x1y81n67x979xf09m_cv580000gn/T/GoLand/___go_build_main_go
2021-08-11T11:08:08.652+0800    info    tuya-pulsar-sdk-go/client.go:67 start creating consumer {"pulsar": "pulsar://mqe.tuyacn.com:7285", "topic": "persistent://xtu7maff*****8ufo/out/event"}

Parse push messages

Turn on the light

  • Received message

    2021-08-11T11:43:45.088+0800    debug   tuya-pulsar-sdk-go/consumer.go:53       consumerImpl receive message    {"topic": "persistent://xtu7maff*****8ufo/out/event-partition-0"}
    2021-08-11T11:43:45.088+0800    info    example/main.go:42      payload preview {"payload": "{\"data\":\"nOFoTwoAb+60lp1Mq1Gi9pTUoNevgMb3n6t6RQeEf2/HceQwrrF0vuCyPhPmcgsLzQtf9wFhkSQ6IBE2P3yYPrIcrc/mOxsa2XLThUj1eW8JfG8gnY2R7wUL3Xn4MP48poM3HepgaRZOkpsSKmAqgdxobOPC7kNpk92hmXjB0PEOZ2Du4MbP4NSN49QcJ4MPgRVaIYpFeWwThl1uFaul6QeTXHlHe0KSqaqKUpDueFTjGH5tallpI10RZxofJ72f\",\"protocol\":4,\"pv\":\"2.0\",\"sign\":\"aa55a7512cae4c7a2170302c64a8152d\",\"t\":1628653425175}"}
    2021-08-11T11:43:45.089+0800    info    example/main.go:58      aes decode      {"decode payload": "{\"dataId\":\"5422f320-fa56-11eb-8396-02425b0322e7\",\"devId\":\"vde*******13176\",\"productKey\":\"pp*****p\",\"status\":[{\"1\":\"true\",\"code\":\"switch_led\",\"t\":1628653425064,\"value\":true}]}"}
    2021-08-11T11:43:45.089+0800    debug   tuya-pulsar-sdk-go/consumer.go:67       Handler trace info      {"msgID": "ledgerId:29026543 entryId:8 partition:0 ", "topic": "persistent://xtu7maff*****8ufo/out/event-partition-0", "decode spend": "14.417µs", "Unactive spend": "1.167µs", "ConsumerID spend": "875ns", "HandlePayload spend": "277.083µs", "Ack spend": "129.541µs", "total spend": "471.75µs"}
    
  • Parsed message data

    aes decode {
    	"decode payload": "{
    		\"dataId\":\"5422f320-fa56-11eb-8396-02425b0322e7\",
    		\"devId\":\"vde*******13176\",
    		\"productKey\":\"pp*****p\",
    		\"status\":[{
    			\"1\":\"true\",
    			\"code\":\"switch_led\",
    			\"t\":1628653425064,
    			\"value\":true	
    		}]
    	}"
    }
    

    Note: For more information about the data parsing method, see Data signing. The data decryption method has been encapsulated in the SDK. For more information, see pkg > tyutils > aes.go.

Adjust the brightness of the light

  • Received message

    2021-08-11T11:39:02.086+0800    debug   tuya-pulsar-sdk-go/consumer.go:53       consumerImpl receive message    {"topic": "persistent://xtu7maff*****8ufo/out/event-partition-0"}
    2021-08-11T11:39:02.089+0800    info    example/main.go:42      payload preview {"payload": "{\"data\":\"YWLA8449ERiXEzbnOzEETWIHXXLCsmCgK01uJteO1xHHceQwrrF0vuCyPhPmcgsLzQtf9wFhkSQ6IBE2P3yYPrIcrc/mOxsa2XLThUj1eW8JfG8gnY2R7wUL3Xn4MP48poM3HepgaRZOkpsSKmAqgSlV4VEv7NtZ0T9wsM0xsQe+Gd3lcDRjtoWkeHTaIhvNoE/5/HN5VRTt8SFceoAp0HbL1+8oe+WxngU4aUzQQUDqlI27xZTlsgXlpK+rmyxy7z0o3iD5KROUiGtsAvdO0XJ+KAh08J5b48fs2hkT/LQxN25CG2nxnLbJVhQsTaeDxs3Ggo1C/+qAL04mfKryUw==\",\"protocol\":4,\"pv\":\"2.0\",\"sign\":\"67d95e7*******1814c4**7fa5b7c01\",\"t\":1628653142175}"}
    2021-08-11T11:39:02.089+0800    info    example/main.go:58      aes decode      {"decode payload": "{\"dataId\":\"ab770414-fa55-11eb-8396-02425b0322e7\",\"devId\":\"vde*******13176\",\"productKey\":\"pp*****p\",\"status\":[{\"2\":\"white\",\"code\":\"work_mode\",\"t\":1628653142080,\"value\":\"white\"},{\"3\":\"507\",\"code\":\"bright_value_v2\",\"t\":1628653142080,\"value\":507}]}"}
    2021-08-11T11:39:02.092+0800    debug   tuya-pulsar-sdk-go/consumer.go:67       Handler trace info      {"msgID": "ledgerId:29026543 entryId:6 partition:0 ", "topic": "persistent://xtu7maff*****8ufo/out/event-partition-0", "decode spend": "68.458µs", "Unactive spend": "1.041µs", "ConsumerID spend": "1.542µs", "HandlePayload spend": "3.312541ms", "Ack spend": "175.791µs", "total spend": "3.5965ms"}
    
    
  • Parsed message data

    aes decode {
    	"decode payload": "{
    		\"dataId\":\"ab770414-fa55-11eb-8396-02425b0322e7\",
    		\"devId\":\"vde*******13176\",
    		\"productKey\":\"pp*****p\",
    		\"status\":[
    			{
    				\"2\":\"white\",
    				\"code\":\"work_mode\",
    				\"t\":1628653142080,
    				\"value\":\"white\"
    			},
    			{
    				\"3\":\"507\",
    				\"code\":\"bright_value_v2\",
    				\"t\":1628653142080,
    				\"value\":507
    			}
    		]
    	}"
    }
    

References

For more information, see Get Push Messages by Pulsar (Java SDK).