robhogan / react-native-paho-mqtt

react-native-paho-mqtt
91 stars 32 forks source link

Sending empty MQTT message #32

Closed j3r1ch0-2007 closed 5 years ago

j3r1ch0-2007 commented 5 years ago

Hi, I am trying to send an empty MQTT message on a specified topic. This is a common pattern with MQTT to 'query' the status of an MQTT connected device.

I am using the project in a React Native app and can connect, subscribe and send (non-empty) messages fine. I am not sure how to send and empty message though.

The below code works fine and sends the payload 0 on the topic specified in device.command:

     const _message = new Message('0');
      _message.destinationName = device.command;
      this.onMessageSendHandler(_message);

In order to try and send empty messages I tried to create the new message object in the following two ways both of which returned errors:

  1. to test not passing and parameter to the object creation

     const _message = new Message();
    • Error received: TypeError: undefined is not and object (evaluating 'newPayload.toString')
  2. to test an empty string:

     const _message = new Message('');
    • Error received: Error: AMQJS0011E Invalid state not connected.
  3. to test an empty array:

     const _message = new Message([]);
    • Error received: AMQJS0013E Invalid argument for newPayload.
  4. to test an empty JavaScript object as a string:

     const _message = new Message('{}');
    • Error received: Error: AMQJS0011E Invalid state not connected.
  5. to test an empty JavaScript object:

     const _message = new Message({});
    • Error received: AMQJS0013E Invalid argument for newPayload.

So I am not sure how to send and empty message with this client library. Did I miss something obvious or not so obvious?

j3r1ch0-2007 commented 5 years ago

Apparently I was having a bad day... this following approach seems to work: const _message = new Message('');