iothub icon indicating copy to clipboard operation
iothub copied to clipboard

Subscribe to C2D events throws error while receiving the events on device side

Open PremSahooESL opened this issue 2 years ago • 17 comments

Go Version: go 1.19

ERROR message parse error: invalid semicolon separator in query

      c, err := iotdevice.NewFromConnectionString(
		iotmqtt.New(), "xxxxxxxxxxxxxxxxxxxx",
	)
	if err != nil {
		log.Fatal(err)
	}

	// connect to the iothub
	if err = c.Connect(context.Background()); err != nil {
		log.Fatal(err)
	}

	// receive a cloud-to-device message
	eventSub, err := c.SubscribeEvents(context.Background())

	for msg := range eventSub.C() {
		fmt.Println(msg.Payload)
	}

	if err := eventSub.Err(); err != nil {
		log.Fatal(err)
	}

PremSahooESL avatar May 29 '23 07:05 PremSahooESL

Something must be wrong with the connection url

amenzhinsky avatar May 29 '23 08:05 amenzhinsky

Connection has been established. It also gets triggered upon events arrival. But with the below error

ERROR message parse error: invalid semicolon separator in query

Below is the sample connection URL I used:

HostName=xxxxxxx.azure-devices.net;SharedAccessKeyName=device;DeviceId=MY_GO_TEST;SharedAccessKey=xxxxxxxxx

PremSahooESL avatar May 29 '23 08:05 PremSahooESL

Hi, @amenzhinsky is there any update?

vishal210893 avatar May 30 '23 09:05 vishal210893

@amenzhinsky Do you have any concerns on above connection string format? If so then how come the client could get authorized with above connection string format. But could not parse the data and throws parse error.

Is this SDK able to receive data in your case?

PremSahooESL avatar Jun 06 '23 12:06 PremSahooESL

@PremSahooESL it seems parseCloudToDeviceTopic fails to parse the topic name of the incoming message, could you log it in your app to investigate what causes the problem?

amenzhinsky avatar Jun 06 '23 15:06 amenzhinsky

Thanks for the response. Sure let me check..

PremSahooESL avatar Jun 07 '23 04:06 PremSahooESL

Below is the Topic Name What I received from: This could not be parsed by parseQuery() function on url.go

$.to=/devices/PREM_GO_TEST/messages/deviceBound&$.ct=text/plain; charset=UTF-8&$.ce=

PremSahooESL avatar Jun 07 '23 05:06 PremSahooESL

Is it something different or unexpected which comes from C2D by IotHub?

PremSahooESL avatar Jun 07 '23 06:06 PremSahooESL

That's odd, there's a preparation of this type of queries for the following parsing.

https://github.com/amenzhinsky/iothub/blob/master/iotdevice/transport/mqtt/mqtt.go#L280

Could you make sure that parseCloudToDeviceTopic throws the error?

amenzhinsky avatar Jun 07 '23 08:06 amenzhinsky

Below code throws the error: parseCloudToDeviceTopic() -> ParseQuery() -> parseQuery()

func parseCloudToDeviceTopic(s string) (map[string]string, error) {
	s, err := url.QueryUnescape(s)
	if err != nil {
		return nil, err
	}

	// attributes prefixed with $.,
	// e.g. `messageId` becomes `$.mid`, `to` becomes `$.to`, etc.
	i := strings.Index(s, "$.")
	if i == -1 {
		return nil, errors.New("malformed cloud-to-device topic name")
	}
	q, err := url.ParseQuery(s[i:])
	if err != nil {
		return nil, err
	}

	p := make(map[string]string, len(q))
	for k, v := range q {
		if len(v) != 1 {
			return nil, fmt.Errorf("unexpected number of property values: %d", len(q))
		}
		p[k] = v[0]
	}
	return p, nil
}

PremSahooESL avatar Jun 07 '23 08:06 PremSahooESL

I could see below line is missing from above code:

https://github.com/amenzhinsky/iothub/blob/master/iotdevice/transport/mqtt/mqtt.go#L280

PremSahooESL avatar Jun 07 '23 08:06 PremSahooESL

https://github.com/amenzhinsky/iothub/commit/7eb906f6251679227fb62b5f05c5c1e3b7316b4f

Could you update to the latest version?

amenzhinsky avatar Jun 07 '23 08:06 amenzhinsky

it works now. Thanks.

PremSahooESL avatar Jun 07 '23 08:06 PremSahooESL

I will keep posting here if I face other issues. Infact this is the only Github repo for Go Lang SDK for Azure IoT Hub. Hope it supports all basic C2D functions.

PremSahooESL avatar Jun 07 '23 08:06 PremSahooESL

i am just trying to send a C-2-D message.

I am trying to use below imports statement. But my application could not start up. It keeps throwing beolow error.

"github.com/amenzhinsky/iothub/iotservice"

image

PremSahooESL avatar Jul 24 '23 10:07 PremSahooESL

Looks.... latest Eventhub client.go in this repo is not using latest amqp 1.0.1 lib

image

PremSahooESL avatar Jul 24 '23 14:07 PremSahooESL

Below is the send() of latest amqp 1.0.1 lib code....

image

PremSahooESL avatar Jul 24 '23 14:07 PremSahooESL