How to use @nestjs/microservices - 10 common examples

To help you get started, we’ve selected a few @nestjs/microservices examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kmturley / angular-nest-grpc / backend / src / hero / hero.controller.ts View on Github external
ClientGrpc,
} from '@nestjs/microservices';
import { Observable, Subject } from 'rxjs';
import { grpcClientOptions } from '../grpc-client.options';
import { HeroById } from './interfaces/hero-by-id.interface';
import { HeroByName } from './interfaces/hero-by-name.interface';
import { Hero, HeroList } from './interfaces/hero.interface';

interface HeroService {
  getHeroById(data: { id: number }): Observable;
  listHeroesByName(data: { name: string }): Observable;
}

@Controller()
export class HeroController implements OnModuleInit {
  @Client(grpcClientOptions) private readonly client: ClientGrpc;
  private heroService: HeroService;
  private items = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Doe' },
    { id: 3, name: 'Billy' },
    { id: 4, name: 'Joey' },
  ];

  onModuleInit() {
    this.heroService = this.client.getService('HeroService');
  }

  // http methods

  @Get()
  call(): Observable {
github new-eden-social / new-eden-social / src / services / notification / server.ts View on Github external
async function bootstrap() {
  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);
github nestjs / terminus / lib / health-indicators / microservice / grpc.health.ts View on Github external
private createClient(
    options: CheckGRPCServiceOptions,
  ): NestJSMicroservices.ClientGrpc {
    const {
      timeout,
      healthServiceName,
      healthServiceCheck,
      ...grpcOptions
    } = options;
    // TODO: Remove `as any` as soon nestjs/nest#2240 gets merged
    return this.nestJsMicroservices.ClientProxyFactory.create({
      transport: Transport.GRPC,
      options: grpcOptions as GrpcOptionsOptions,
    }) as any;
  }
github new-eden-social / new-eden-social / src / services / alliance / server.ts View on Github external
async function bootstrap() {
  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(AllianceModule);

  app.connectMicroservice({
    transport: Transport.GRPC,
    options: {
      url: `0.0.0.0:${GRPC_PORT}`,
      package: 'alliance',
      protoPath: join(__dirname, 'src/grpc/alliance.proto'),
    },
  });

  await app.startAllMicroservicesAsync();
  await app.listen(HTTP_PORT);
}
github new-eden-social / new-eden-social / src / services / follow / server.ts View on Github external
async function bootstrap() {
  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(FollowModule);

  app.connectMicroservice({
    transport: Transport.GRPC,
    options: {
      url: `0.0.0.0:${GRPC_PORT}`,
      package: 'follow',
      protoPath: join(__dirname, 'src/grpc/follow.proto'),
    },
  });

  await app.startAllMicroservicesAsync();
  await app.listen(HTTP_PORT);
}
github new-eden-social / new-eden-social / src / services / comment / server.ts View on Github external
async function bootstrap() {
  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(CommentModule);

  app.connectMicroservice({
    transport: Transport.GRPC,
    options: {
      url: `0.0.0.0:${GRPC_PORT}`,
      package: 'post',
      protoPath: join(__dirname, 'src/grpc/post.proto'),
    },
  });

  await app.startAllMicroservicesAsync();
  await app.listen(HTTP_PORT);
}
github new-eden-social / new-eden-social / src / services / hashtag / server.ts View on Github external
async function bootstrap() {
  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(HashtagModule);

  app.connectMicroservice({
    transport: Transport.GRPC,
    options: {
      url: `0.0.0.0:${GRPC_PORT}`,
      package: 'hashtag',
      protoPath: join(__dirname, 'src/grpc/hashtag.proto'),
    },
  });

  await app.startAllMicroservicesAsync();
  await app.listen(HTTP_PORT);
}
github new-eden-social / new-eden-social / src / services / corporation / server.ts View on Github external
async function bootstrap() {
  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(CorporationModule);

  app.connectMicroservice({
    transport: Transport.GRPC,
    options: {
      url: `0.0.0.0:${GRPC_PORT}`,
      package: 'corporation',
      protoPath: join(__dirname, 'src/grpc/corporation.proto'),
    },
  });

  await app.startAllMicroservicesAsync();
  await app.listen(HTTP_PORT);
}
bootstrap();
github new-eden-social / new-eden-social / src / services / notification / server.ts View on Github external
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);
}
github kmturley / angular-nest-grpc / backend / src / hero / hero.controller.ts View on Github external
}

  // grpc streaming methods

  @GrpcStreamMethod('HeroService')
  async getHeroByIdStream(messages: Observable): Promise {
    return new Promise((resolve, reject) => {
      messages.subscribe(msg => {
        resolve(this.items.find(({ id }) => id === msg.id));
      }, err => {
        reject(err);
      });
    });
  }

  @GrpcStreamMethod('HeroService')
  async listHeroesByNameStream(messages: Observable): Promise {
    return new Promise((resolve, reject) => {
      messages.subscribe(msg => {
        resolve({ heroes: this.items.filter(({ name }) => name.startsWith(msg.name)) });
      }, err => {
        reject(err);
      });
    });
  }

}