Stream.on()
Open source
Stream.on()
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 | EventHandler | 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';
const client = new Client();
client.load([], '../../grpc_server/route_guide.proto');
export default () => {
if (__ITER == 0) {
client.connect('127.0.0.1:10000', { plaintext: true });
}
const stream = new Stream(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);
};
Was this page helpful?
Related documentation
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
24 Apr

Getting started with Grafana dashboard design
In this webinar, you'll learn how to design stylish and easily accessible Grafana dashboards that tell a story.
08 May

Grafana 12 deep dive
It wouldn’t be GrafanaCON without a new release. Join us as we dive into the latest features of Grafana 12, including dashboards as code, Git-based configs, dynamic dashboards, and more.
08 May

Monitoring EA App’s 200+ core error metrics with a scalable, color-coded Grafana dashboard
Learn how engineers at Electronic Arts built a Grafana dashboard that client teams can use to monitor 200+ core error metrics.