The countdown is on: Register today + save your seatLearn how to leverage new AI features and observability tools, attend technical deep dives. & leave with tips for growing your observability strategy. Seats are limited and in high demand, register now!
📢 Registration + agenda now live
Explore the latest Grafana Cloud and AI solutions, learn tips & tricks from demos and hands-on workshops, and get actionable advice to advance your observability strategy. Register now and get 50% off - limited tickets available (while they last!).
Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
Set up handler functions for various events on the gRPC stream.
Parameter
Type
Description
event
string
The event name to define a handler for.
handler
function
The function to call when the event happens.
Possible events:
Event name
Description
data
Emitted when the server sends data.
error
Emitted when an error occurs. In case of the error, an
Error object sends to the handler function.
end
Emitted when the server closes the incoming stream.
Example
JavaScript
import{ Client, Stream }from'k6/net/grpc';import{ sleep }from'k6';constCOORD_FACTOR=1e7;const client =newClient();
client.load([],'../../grpc_server/route_guide.proto');exportdefault()=>{if(__ITER ==0){
client.connect('127.0.0.1:10000',{plaintext:true});}const stream =newStream(client,'main.RouteGuide/RecordRoute');// sets up a handler for the data (server sends data) event
stream.on('data',(stats)=>{
console.log('Finished trip with', stats.pointCount,'points');
console.log('Passed', stats.featureCount,'features');
console.log('Traveled', stats.distance,'meters');
console.log('It took', stats.elapsedTime,'seconds');});// sets up a handler for the end event (stream closes)
stream.on('end',function(){// The server has finished sending
client.close();
console.log('All done');});// sets up a handler for the error event (an error occurs)
stream.on('error',function(e){// An error has occurred and the stream has been closed.
console.log('Error: '+JSON.stringify(e));});sleep(1);};