Skip to content

Instantly share code, notes, and snippets.

@andrewloable
Last active November 13, 2020 12:19
Show Gist options
  • Save andrewloable/a46d76bc360dc87795a67aea88194006 to your computer and use it in GitHub Desktop.
Save andrewloable/a46d76bc360dc87795a67aea88194006 to your computer and use it in GitHub Desktop.
Sample grpc-web in angular
import { Component, OnInit } from '@angular/core';
import { grpc } from '@improbable-eng/grpc-web';
import { HelloReply, HelloRequest } from './grpc/src/proto/greet_pb';
import { Greeter } from './grpc/src/proto/greet_pb_service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'ng-grpc-test';
// This function to be implemented in a service
getValue(): Promise<HelloReply.AsObject> {
return new Promise( (resolve, reject) => {
const param = new HelloRequest();
param.setName('Wawarts');
grpc.unary(Greeter.SayHello, {
request: param,
host: 'https://localhost:5001',
onEnd: resp => {
const { status, message } = resp;
if (status === grpc.Code.OK && message) {
var retval = message.toObject() as HelloReply.AsObject;
resolve(retval);
} else {
reject(resp);
}
}
});
});
}
async getval() : Promise<void> {
var ret = await this.getValue();
console.log(ret.message);
}
ngOnInit(): void {
this.getval();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment