Reputation: 141
I was trying to implement a simple grpc service in my nest js project but unfortunately my client is not communicating with the server. I am sharing some codes below. It will be really helpful if anyone can give me some insights of whats going wrong.
--------- GRPC Server -------
Main.ts file looks like this.
async function setUpGrpc(grpcUrl){
const grpcApp = await NestFactory.createMicroservice<MicroserviceOptions>(GrpcModule, {
transport: Transport.GRPC,
options: {
package: 'protos.user',
url : grpcUrl,
protoPath: join(__dirname, '/protos/user.proto'),
},
});
return grpcApp;
}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const port = configService.get('PORT');
const grpcUrl = configService.get('GRPC_URL');
console.log(grpcUrl);
const grpcApp = await setUpGrpc(grpcUrl);
await grpcApp.listen(()=>{
console.log("Grpc is listening")
});
await app.listen(port);
}
bootstrap();
GrpcModule
@Module({
imports: [
SequelizeDbModule,
],
controllers: [GrpcUserController],
providers: [],
})
export class GrpcModule {}
GrpcUserController
interface IUserInfo {
userName: string;
passWord: string;
tenantId: string;
}
interface IUser {
id: string;
name: string;
email: string;
phone: string;
tenantId: string;
}
@Controller()
export class GrpcUserController {
constructor() {}
@GrpcMethod('UserValidationService', 'ValidateUser')
validateUser(userInfo: IUserInfo, metaData: any) : IUser {
console.log('Call is made');
return {name:'Test',phone:'Test',email:"Test@Test",tenantId:"TestTenant",id:'TestId'};
}
}
My Proto file looks like this.
syntax = "proto3";
package protos.user;
service UserValidationService {
rpc ValidateUser (UserInfo) returns (User) {}
}
message UserInfo {
string userName = 1;
string passWord = 2;
string tenantId = 3;
}
message User {
string id = 1;
string name = 2;
string email = 3;
string phone = 4;
string tenantId = 5;
}
------ Client --------
Used the same protofile
I made the client as a nest library
@Injectable()
export class UserGrpcClientService implements OnModuleInit {
userValidationService:IUserValidationService;
@Client( {
transport: Transport.GRPC,
options: {
package: 'protos.user',
url : "localhost:5000",
protoPath: join(__dirname, '/protos/user.proto'),
},
})
client : ClientGrpc
constructor(){}
onModuleInit() {
this.userValidationService = this.client.getService<IUserValidationService('UserValidationService');
console.log(this.userValidationService);
}
validateUser(userInfo: IUserInfo) : Observable<any>{
console.log("Inside validateUser");
console.log(JSON.stringify(userInfo));
return this.userValidationService.validateUser({userName:userInfo.userName,
passWord:userInfo.passWord,
tenantId:userInfo.tenantId});
}
}
interfaces
export interface IUserInfo {
userName: string;
passWord: string;
tenantId: string;
}
export interface IUser {
id: string;
name: string;
email: string;
phone: string;
tenantId: string;
}
export interface IUserValidationService{
validateUser (UserInfo : IUserInfo) : Observable<any>
}
I have integrated this client library in another running nest application.
@Post('auth/token')
@HttpCode(200)
async token(
@Body() tokenRequest: TokenRequestDTO,
@Headers() headers,
): Promise<Object> {
this.logger.info('Inside token request');
this.helper.validateRequestBody(tokenRequest);
this.helper.validateHeader(headers);
var response = this.userGrpcClient.validateUser({
userName: 'test',
passWord: 'test',
tenantId: 'test',
})
console.log(response);
return await this.authService.provideToken(tokenRequest, headers);
}
when I run this, there is no error. the response that I logged shows Observable { _isScalar: false, _subscribe: [Function] }
Upvotes: 0
Views: 2521
Reputation: 141
It turns out that when calling the validateUser function using the userGrpcClient it returns an observable and I have to subscribe on it.
Upvotes: 0