@nestjs/microservices
Version:
Microservices for NestJS: message and event handlers over Kafka, RabbitMQ, NATS, Redis, MQTT, gRPC or TCP.
37 lines (36 loc) • 1.11 kB
JavaScript
import { isObject } from '@nestjs/common/internal';
import { toPacketMetadata } from '../utils/packet-metadata.utils.js';
import { BaseRpcContext } from './base-rpc.context.js';
/**
* @publicApi
*/
export class MqttContext extends BaseRpcContext {
constructor(args) {
super(args);
}
/**
* Returns the name of the topic.
*/
getTopic() {
return this.args[0];
}
/**
* Returns the reference to the original MQTT packet.
*/
getPacket() {
return this.args[1];
}
/**
* Returns the packet's MQTT 5 user properties as packet metadata: what the
* client attached to the packet, and every other user property with a
* single value. A property with several values is only available through
* `getPacket()`. MQTT 3 has no user properties, so there is nothing to
* return over it.
*/
getMetadata() {
const userProperties = this.args[1]?.properties?.userProperties;
return isObject(userProperties)
? toPacketMetadata(Object.entries(userProperties))
: undefined;
}
}