carlessistare / grpc-promise Goto Github PK
View Code? Open in Web Editor NEWGRPC promisify module for all Request/Response types: standard and stream
License: MIT License
GRPC promisify module for all Request/Response types: standard and stream
License: MIT License
With the most recent grpc update the library now crashes with the error
TypeError: requestTypes[originalFunction.name] is not a function
In bidirectional example on client-side there is a call to .end()
method which always results in a timeout. Happens when executing the client when there is a delay on server-side.
I recommend moving it to the callback of .sendMessage({})
method after outputting the results:
t = client.streamAction();
t.sendMessage({})
.then(res => {
console.log('Client: Stream Message Received = ', res); // Client: Stream Message Received = {id: 1}
t.end();
})
.catch(err => console.error(err));
Thanks for the great module.
Edit: Ending would also be required in .catch()
, so I would suggest perhaps using async/await syntax to prevent DRY. The following simple example should work on client-side:
try {
t = client.streamAction();
const res = await t.sendMessage({});
console.log(res);
}
catch(err) {
console.log(err);
}
t.end();
Since I use grpc_tools_node_protoc_ts plugin for protoc to generate TypeScript codes. Is there any plan for supporting TS and integrating with this package? Thanks!
After promisifying all functions in the RPC, how do I access the original callback function, if I desire to use both promise and callback versions?
** Great library, by the way...
Currently, a promisified streaming-response function returns a promise that resolves when the server ends the stream. One use-case of streaming-response RPCs is, however, that the client processes the reponse stream while the server is still executing the response. With the current implemenation, this is not possible, as the client receives the data in one go, when the stream completes.
This also makes it impossible to use infinite streams, as that causes the client to wait indefinitely.
Minimal example, JS client:
const grpc = require('grpc')
const grpcPromise = require('grpc-promise')
const protoLoader = require('@grpc/proto-loader')
const packageDefinition = protoLoader.loadSync(...)
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition)
const client = new protoDescriptor.ExampleService('localhost:50051', grpc.credentials.createInsecure())
grpcPromise.promisifyAll(client)
client.exampleUnaryStream()
.sendMessage({})
.then(r => console.log(`RESPONSE ${JSON.stringify(r)}`))
.catch(e => console.log(`ERROR ${JSON.stringify(e)}`))
Python Server (based on grpclib and betterproto, see this issue for details; this part is working and not the cause of the problem mentioned here):
class ExampleService(ServiceStub):
service_name = "ExampleService"
@rpc_unary_stream(name="ExampleUnaryStream")
async def example_unary_stream(self, request: Empty) -> AsyncIterator[Empty]:
for i in range(5):
yield Empty()
await asyncio.sleep(1)
server = Server([ExampleService()])
await server.start("localhost", 50051)
await server.wait_closed()
As a temporary work-around, I only promisify unary/unary functions:
const unaryFunctions = []
Object.keys(Object.getPrototypeOf(client)).forEach(functionName => {
if (client[functionName].requestStream === false && client[functionName].responseStream === false) {
unaryFunctions.push(functionName)
}
})
grpcPromise.promisify(client, unaryFunctions)
I'm not quite sure how this could be done with promises but it would be nice to be able to register an onData
callback somehow and still retain the nicer syntax that the promisified functions provide.
hi, how to setup https proxy for grpc-promise client? i dont saw any relative comments in the code. So im asking here.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.