Client.connect(address [,params])
Open source
Client.connect(address [,params])
Opens a connection to a gRPC server; will block until a connection is made or a connection error is thrown. Cannot be called during the init
phase.
See Client.close() to close the connection.
Parameter | Type | Description |
---|---|---|
address | string | The address of the gRPC server. Should be in the form: host:port with no protocol prefix e.g. grpc.k6.io:443 . The host must be a literal IP address, or a host name that can be resolved to IP addresses. The port must be a literal port number or a service name e.g. :443 or :https . If the host is a literal IPv6 address it must be enclosed in square brackets, as in [2001:db8::1]:80 or [fe80::1%zone]:80 . |
params (optional) | object | ConnectParams object containing additional connect parameters. |
ConnectParams
Name | Type | Description |
---|---|---|
ConnectParams.plaintext | bool | If true will connect to the gRPC server using plaintext i.e. insecure. Defaults to false i.e. secure via TLS. |
ConnectParams.reflect | boolean | Whether to use the gRPC server reflection protocol when connecting. |
ConnectParams.reflectMetadata | object | Object with key-value pairs representing custom metadata the user would like to add to the reflection request. |
ConnectParams.timeout | string / number | Connection timeout to use. Default timeout is "60s" .The type can also be a number, in which case k6 interprets it as milliseconds, e.g., 60000 is equivalent to "60s" . |
ConnectParams.maxReceiveSize | number | Sets the maximum message size in bytes the client can receive. Defaults to grpc-go default, which is 4MB. |
ConnectParams.maxSendSize | number | Sets the maximum message size in bytes the client can send. Defaults to grpc-go default, which is approximately 2GB. |
ConnectParams.tls (optional) | object | TLS settings of the connection. Defaults to null . |
TLS
TLS settings of the connection. If not defined, the main TLS config from options will be used.
Name | Type | Description |
---|---|---|
tls.cert | string | PEM formatted client certificate. |
tls.key | string | PEM formatted client private key. |
tls.password | string | Password for decrypting the client’s private key. |
tls.cacerts | string / array | PEM formatted strings of the certificate authorities. |
Examples
JavaScript
import grpc from 'k6/net/grpc';
const client = new grpc.Client();
export default () => {
client.connect('localhost:8080');
};
JavaScript
import grpc from 'k6/net/grpc';
const client = new grpc.Client();
export default () => {
client.connect('localhost:8080', { plaintext: true });
};
JavaScript
import grpc from 'k6/net/grpc';
import { check } from 'k6';
import { SharedArray } from 'k6/data';
import exec from 'k6/execution';
// note: the services in this example don't exist. If you would like
// to run this example, make sure to replace the URLs, and
// the cacerts, cert, key, and password variables.
const grpcArgs = new SharedArray('grpc', () => {
// Using SharedArray here so that not every VU gets a copy of every certificate a key
return [
{
host: 'foo1.grpc-quickpizza.grafana.com:443',
plaintext: false,
params: {
tls: {
cacerts: [open('cacerts0.pem')],
cert: open('cert0.pem'),
key: open('key0.pem'),
},
},
},
{
host: 'foo2.grpc-quickpizza.grafana.com:443',
params: {
plaintext: false,
tls: {
cacerts: open('cacerts1.pem'),
cert: open('cert1.pem'),
key: open('key1.pem'),
password: 'cert1-passphrase',
},
},
},
];
});
const client = new grpc.Client(null, 'quickpizza.proto');
export default () => {
if (__ITER === 0) {
// Take one config and use it for this one VU
const grpcArg = grpcArgs[exec.vu.idInTest % grpcArgs.length];
client.connect(grpcArg.host, grpcArg.params);
}
const response = client.invoke('quickpizza.GRPC/Status');
check(response, {
'status is OK': (r) => r && r.status === grpc.StatusOK,
});
console.log(JSON.stringify(response.message));
};
Was this page helpful?
Related documentation
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
08 May

k6 1.0: How this long-awaited major release makes it easier to get started with testing
Grafana k6 v1.0 is here! k6 contributors will demonstrate features in the open source load testing tool, such as native extensibility (no more xk6 workarounds); the OpenAPI converter to streamline and automate the creation of TypeScript-based tests; and k6 Studio, a desktop application that simplifies performance testing for everyone.
60 min

Performance testing and observability in Grafana Cloud
In this webinar, learn how Grafana Cloud k6 offers you the best developer experience for performance testing.
60 min

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.