hubcio commented on code in PR #2165: URL: https://github.com/apache/iggy/pull/2165#discussion_r2550239272
########## examples/nodejs/src/basic-js/producer.js: ########## @@ -0,0 +1,168 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Client, Partitioning } from 'apache-iggy'; + +const log = console.log; + +const STREAM_ID = 1; +const TOPIC_ID = 1; +const PARTITION_ID = 1; +const BATCHES_LIMIT = 5; +const MESSAGES_PER_BATCH = 10; + +function parseArgs() { + const args = process.argv.slice(2); + const connectionString = args[0] || 'iggy+tcp://iggy:[email protected]:8090'; + + if (args.length > 0 && (args[0] === '-h' || args[0] === '--help')) { + log('Usage: node producer.js [connection_string]'); + log('Example: node producer.js iggy+tcp://iggy:[email protected]:8090'); + process.exit(0); + } + + return { connectionString }; +} + +async function initSystem(client) { + try { + log('Creating stream with ID %d...', STREAM_ID); + await client.stream.create({ streamId: STREAM_ID, name: 'sample-stream' }); + log('Stream was created successfully.'); + } catch (error) { + log('Stream already exists or error creating stream: %o', error); + } + + try { + log('Creating topic with ID %d in stream %d...', TOPIC_ID, STREAM_ID); + await client.topic.create({ + streamId: STREAM_ID, + topicId: TOPIC_ID, + name: 'sample-topic', + partitionCount: 1, + compressionAlgorithm: 1, // None + replicationFactor: 1 + }); + log('Topic was created successfully.'); + } catch (error) { + log('Topic already exists or error creating topic: %o', error); + } + + // Server ACK confirms creation; no extra wait needed +} + +async function produceMessages(client) { + const interval = 500; // 500 milliseconds + log( + 'Messages will be sent to stream: %d, topic: %d, partition: %d with interval %d ms.', + STREAM_ID, + TOPIC_ID, + PARTITION_ID, + interval + ); + + let currentId = 0; + let sentBatches = 0; + + while (sentBatches < BATCHES_LIMIT) { + const messages = []; + const sentMessages = []; Review Comment: @T1B0 what do you think? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
