Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [MqttController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.MQTT,
options: {
url: 'mqtt://0.0.0.0:1883',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});
it(`MQTT`, () => {
return request(server)
.post('/')
.send({
transport: Transport.MQTT,
options: {
host: 'mqtt://broker.hivemq.com',
port: 183,
},
})
.expect(408);
});
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [MqttBroadcastController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.MQTT,
options: {
host: '0.0.0.0',
},
});
app.connectMicroservice({
transport: Transport.MQTT,
options: {
host: '0.0.0.0',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [MqttBroadcastController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.MQTT,
options: {
host: '0.0.0.0',
},
});
app.connectMicroservice({
transport: Transport.MQTT,
options: {
host: '0.0.0.0',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});
import { Controller, Get } from '@nestjs/common';
import {
Client,
ClientProxy,
MessagePattern,
Transport,
} from '@nestjs/microservices';
import { Observable } from 'rxjs';
import { scan, take } from 'rxjs/operators';
@Controller()
export class MqttBroadcastController {
@Client({ transport: Transport.MQTT })
client: ClientProxy;
@Get('broadcast')
multicats() {
return this.client.send({ cmd: 'broadcast' }, {}).pipe(
scan((a, b) => a + b),
take(2),
);
}
@MessagePattern({ cmd: 'broadcast' })
replyBroadcast(): Observable {
return new Observable(observer => observer.next(1));
}
}
import { Body, Controller, HttpCode, Post, Query } from '@nestjs/common';
import {
Client,
ClientProxy,
EventPattern,
MessagePattern,
Transport,
} from '@nestjs/microservices';
import { from, Observable, of } from 'rxjs';
import { scan } from 'rxjs/operators';
@Controller()
export class MqttController {
static IS_NOTIFIED = false;
@Client({ transport: Transport.MQTT })
client: ClientProxy;
@Post()
@HttpCode(200)
async call(
@Query('command') cmd,
@Body() data: number[],
): Promise> {
await this.client.connect();
return this.client.send({ cmd }, data);
}
@Post('stream')
@HttpCode(200)
stream(@Body() data: number[]): Observable {
return this.client