nest-mqtt icon indicating copy to clipboard operation
nest-mqtt copied to clipboard

Issue when publishing binary buffer or Uint8Array

Open andreap2018 opened this issue 3 years ago • 1 comments

When trying to publish a proto binary buffer the object is converted to JSON string, but it should be published as it is since the client supports buffer / Uint8Array

One way to do it is to change the condition as follow:

  if (typeof message === 'object' && !Buffer.isBuffer(message)) {
    message = JSON.stringify(message);
  }

Is it possible to apply this change ? Thanks Andrea

Code from mqtt.service.ts :

  publish(topic: string, message: string | Buffer | object, opts?: IClientPublishOptions): Promise<Packet> {
    return new Promise<Packet>((resolve, reject) => {
      if (typeof message === 'object') {
        message = JSON.stringify(message);
      }
      this.client.publish(topic, message, opts || null, (error, packet) => {
        if (error) {
          reject(error);
        } else {
          resolve(packet);
        }
      });
    });
  }

andreap2018 avatar Jun 29 '21 05:06 andreap2018

Thanks for your opinion. Welcome for your pull requests.

microud avatar Jul 06 '21 10:07 microud