Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-net-grpc/stream/stream-on.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-net-grpc/stream/stream-on/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
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. |
Note
When you enable the experimental
async-metric-contextfeature flag, eachStream.on()handler captures the tags and metadata active when you register it. Metrics emitted by the handler use a copy of that context, and k6 restores the interrupted context after the handler returns or throws. Promise reactions andawaitcontinuations created by the handler keep its context.
Example
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 resources from Grafana Labs

