Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(NotificationModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'notification',
protoPath: join(__dirname, 'src/grpc/notification.proto'),
},
});
app.connectMicroservice({
transport: Transport.REDIS,
options: {
url: process.env.REDIS_HOST,
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
}
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [RedisController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.REDIS,
});
await app.startAllMicroservicesAsync();
await app.init();
});
it(`REDIS`, () => {
return request(server)
.post('/')
.send({
transport: Transport.REDIS,
options: {
url: 'redis://localhost:3333',
},
})
.expect(408);
});
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [RedisBroadcastController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.REDIS,
options: {
url: 'redis://0.0.0.0:6379',
},
});
app.connectMicroservice({
transport: Transport.REDIS,
options: {
url: 'redis://0.0.0.0:6379',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});
import { Injectable } from '@nestjs/common';
import { Client, ClientProxy, Transport } from '@nestjs/microservices';
import { CatalogItem } from 'schemas/graphql';
const REDIS_HOST = process.env.REDIS_HOST || 'localhost';
const REDIS_PORT = process.env.REDIS_PORT || 6379;
@Injectable()
export class CatalogItemService {
@Client({
transport: Transport.REDIS,
options: { url: `redis://${REDIS_HOST}:${REDIS_PORT}` }
})
client: ClientProxy;
public async getCatalogItems(): Promise {
const response = await this.client.send(
{ type: 'get-catalog-items' },
{ someImaginaryParams: 42 }
);
return response.toPromise();
}
}
async () =>
microservice.pingCheck('redis', {
transport: Transport.REDIS,
options: {
url: 'redis://localhost:6379',
},
}),
],
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 RedisController {
static IS_NOTIFIED = false;
@Client({ transport: Transport.REDIS })
client: ClientProxy;
@Post()
@HttpCode(200)
call(@Query('command') cmd, @Body() data: number[]): Observable {
return this.client.send({ cmd }, data);
}
@Post('stream')
@HttpCode(200)
stream(@Body() data: number[]): Observable {
return this.client
.send({ cmd: 'streaming' }, data)
.pipe(scan((a, b) => a + b));
}
import { Controller, Get } from '@nestjs/common';
import {
Client,
MessagePattern,
ClientProxy,
Transport,
} from '@nestjs/microservices';
import { Observable } from 'rxjs';
import { scan, take } from 'rxjs/operators';
@Controller()
export class RedisBroadcastController {
@Client({ transport: Transport.REDIS })
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));
}
}