Skip to content

Commit

Permalink
Set client ID in samples (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfod authored Dec 30, 2024
1 parent 2d189a2 commit 71ebf28
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions samples/browser/pub_sub_mqtt5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ function createClient(provider: AWSCognitoCredentialsProvider) : mqtt5.Mqtt5Clie
wsConfig
)

builder.withConnectProperties({
clientId: "test-" + Math.floor(Math.random() * 100000000)
});

let client : mqtt5.Mqtt5Client = new mqtt5.Mqtt5Client(builder.build());

client.on('error', (error) => {
Expand Down
3 changes: 3 additions & 0 deletions samples/browser/react_sample/src/PubSub5.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function createClient(provider: AWSCognitoCredentialsProvider) : mqtt5.Mqtt5Clie
AWS_IOT_ENDPOINT,
wsConfig
)
builder.withConnectProperties({
clientId: "test-" + Math.floor(Math.random() * 100000000)
});

let client : mqtt5.Mqtt5Client = new mqtt5.Mqtt5Client(builder.build());

Expand Down
2 changes: 1 addition & 1 deletion samples/node/pub_sub_electron_node/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function createClientConfig(isWebsocket: boolean): mqtt5.Mqtt5ClientConfig {

builder.withConnectProperties({
keepAliveIntervalSeconds: 1200,
clientId: "test-client"
clientId: "test-client" + Math.floor(Math.random() * 100000000)
});

return builder.build();
Expand Down
12 changes: 10 additions & 2 deletions samples/node/pub_sub_mqtt5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ yargs.command('*', false, (yargs: any) => {
type: 'string',
required: false
})
.option('client_id', {
alias: 'C',
description: 'Client ID for MQTT connection.',
type: 'string',
required: false
})
}, main).parse();

function creatClientConfig(args : any) : mqtt5.Mqtt5ClientConfig {
function createClientConfig(args : any) : mqtt5.Mqtt5ClientConfig {
let builder : iot.AwsIotMqtt5ClientConfigBuilder | undefined = undefined;

if (args.key && args.cert) {
Expand All @@ -61,7 +67,9 @@ function creatClientConfig(args : any) : mqtt5.Mqtt5ClientConfig {
);
}


builder.withConnectProperties({
clientId: args.client_id || "test-" + Math.floor(Math.random() * 100000000),
keepAliveIntervalSeconds: 1200
});

Expand All @@ -70,7 +78,7 @@ function creatClientConfig(args : any) : mqtt5.Mqtt5ClientConfig {

function createClient(args: any) : mqtt5.Mqtt5Client {

let config : mqtt5.Mqtt5ClientConfig = creatClientConfig(args);
let config : mqtt5.Mqtt5ClientConfig = createClientConfig(args);

console.log("Creating client for " + config.hostName);
let client : mqtt5.Mqtt5Client = new mqtt5.Mqtt5Client(config);
Expand Down

0 comments on commit 71ebf28

Please sign in to comment.