You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: use async functions in gRPC streaming examples
Streaming event handlers can only fire when the call stack is empty,
so synchronous default functions prevent them from running. Update the
server and client streaming examples to use async/await with Promises
and add a short explanation of why async is required.
Applies to next/ and v1.6.x.
Made-with: Cursor
`Found feature called "${feature.name}" at ${feature.location.latitude/COORD_FACTOR}, ${
114
-
feature.location.longitude/COORD_FACTOR
115
-
}`
116
-
);
117
-
});
118
-
119
-
stream.on('end', function () {
120
-
// The server has finished sending
121
-
client.close();
122
-
console.log('All done');
119
+
awaitnewPromise((resolve, reject) => {
120
+
stream.on('data', function (feature) {
121
+
console.log(
122
+
`Found feature called "${feature.name}" at ${feature.location.latitude/COORD_FACTOR}, ${
123
+
feature.location.longitude/COORD_FACTOR
124
+
}`
125
+
);
126
+
});
127
+
128
+
stream.on('error', function (e) {
129
+
reject(e);
130
+
});
131
+
132
+
stream.on('end', function () {
133
+
client.close();
134
+
console.log('All done');
135
+
resolve();
136
+
});
137
+
138
+
stream.write({
139
+
lo: {
140
+
latitude:400000000,
141
+
longitude:-750000000,
142
+
},
143
+
hi: {
144
+
latitude:420000000,
145
+
longitude:-730000000,
146
+
},
147
+
});
123
148
});
124
-
125
-
// send a message to the server
126
-
stream.write({
127
-
lo: {
128
-
latitude:400000000,
129
-
longitude:-750000000,
130
-
},
131
-
hi: {
132
-
latitude:420000000,
133
-
longitude:-730000000,
134
-
},
135
-
});
136
-
137
-
sleep(0.5);
138
-
};
149
+
}
139
150
```
140
151
141
152
{{< /code >}}
142
153
143
-
In the example script, k6 connects to a gRPC server, creates a stream, and sends a message to the server with latitude and longitude coordinates. When the server sends data back, it logs the feature name and its location. When the server finishes sending data, it closes the client connection and logs a completion message.
154
+
In this example, k6 connects to a gRPC server, creates a stream, and sends a message with latitude and longitude coordinates.
155
+
The `async` default function wraps the stream logic in a `Promise`, which lets event handlers fire while the function awaits.
156
+
When the server finishes sending data, the `end` handler closes the client and resolves the promise.
144
157
145
158
### Client gRPC streaming
146
159
147
160
The client streaming mode is the opposite of the server streaming mode. The client sends multiple requests to the server, and the server replies with a single response.
148
161
149
-
The example below demonstrates client streaming.
162
+
The following example demonstrates client streaming:
`Visiting point ${point.name}${point.location.latitude/COORD_FACTOR}, ${
216
-
point.location.longitude/COORD_FACTOR
217
-
}`
218
-
);
219
-
220
-
// send the location to the server
221
-
stream.write(point.location);
222
-
223
-
sleep(0.5);
224
-
};
232
+
}
225
233
```
226
234
227
235
{{< /code >}}
228
236
229
-
In the example script, k6 establishes a connection to a gRPC server, creates a stream, and sends three random points. The server responds with statistics about the trip, which are logged to the console. The code also handles the end of the stream, closing the client and logging a completion message.
237
+
In this example, k6 connects to a gRPC server, creates a stream, and sends three random points.
238
+
The code wraps the stream logic in a `Promise`, the same pattern as the server streaming example.
239
+
The server responds with trip statistics, and the `end` handler closes the client and resolves the promise.
0 commit comments