This is an automated email from the ASF dual-hosted git repository.

jinrongtong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 5b54c566 nodeJs demo (#975)
5b54c566 is described below

commit 5b54c566f4b5eebc1eb6ac69b17ba75360189eb9
Author: zhaohai <[email protected]>
AuthorDate: Thu Mar 19 15:54:27 2026 +0800

    nodeJs demo (#975)
    
    * add demo
    
    * update async
    
    * update TransactionResolution import
    
    * remove key
    
    * fix Removing old Proto Files
    
    ---------
    
    Co-authored-by: zh378814 <[email protected]>
---
 ...eConsumer.ts => ProducerDelayMessageExample.ts} | 37 +++++++++-------
 ...leConsumer.ts => ProducerFifoMessageExample.ts} | 37 +++++++++-------
 ...Consumer.ts => ProducerNormalMessageExample.ts} | 36 +++++++++-------
 nodejs/examples/ProducerSingleton.ts               | 38 +++++++++++++++++
 .../examples/ProducerTransactionMessageExample.ts  | 49 ++++++++++++++++++++++
 nodejs/examples/SimpleConsumer.ts                  | 35 +++++++++-------
 nodejs/scripts/build-grpc.sh                       | 19 ++++++++-
 7 files changed, 190 insertions(+), 61 deletions(-)

diff --git a/nodejs/examples/SimpleConsumer.ts 
b/nodejs/examples/ProducerDelayMessageExample.ts
similarity index 60%
copy from nodejs/examples/SimpleConsumer.ts
copy to nodejs/examples/ProducerDelayMessageExample.ts
index fb36c38d..2985dc83 100644
--- a/nodejs/examples/SimpleConsumer.ts
+++ b/nodejs/examples/ProducerDelayMessageExample.ts
@@ -15,20 +15,27 @@
  * limitations under the License.
  */
 
-import { SimpleConsumer } from '..';
+import { Producer } from '..';
+import { topics, endpoints, sessionCredentials, namespace } from 
'./ProducerSingleton';
 
-const simpleConsumer = new SimpleConsumer({
-  consumerGroup: 'nodejs-demo-group',
-  endpoints: '127.0.0.1:8081',
-  namespace: '',
-  subscriptions: new Map().set('TopicTest', 'nodejs-demo'),
-});
-await simpleConsumer.startup();
+(async () => {
+  const producer = new Producer({
+    endpoints,
+    namespace,
+    sessionCredentials,
+    maxAttempts: 2,
+  });
+  await producer.startup();
+
+  const receipt = await producer.send({
+    topic: topics.delay,
+    tag: 'nodejs-delay',
+    delay: 2000,
+    body: Buffer.from(JSON.stringify({
+      hello: 'rocketmq-client-nodejs world 😄',
+      now: Date(),
+    })),
+  });
+  console.log(receipt);
+})();
 
-const messages = await simpleConsumer.receive(20);
-console.log('got %d messages', messages.length);
-for (const message of messages) {
-  console.log(message);
-  console.log('body=%o', message.body.toString());
-  await simpleConsumer.ack(message);
-}
diff --git a/nodejs/examples/SimpleConsumer.ts 
b/nodejs/examples/ProducerFifoMessageExample.ts
similarity index 59%
copy from nodejs/examples/SimpleConsumer.ts
copy to nodejs/examples/ProducerFifoMessageExample.ts
index fb36c38d..e3450b57 100644
--- a/nodejs/examples/SimpleConsumer.ts
+++ b/nodejs/examples/ProducerFifoMessageExample.ts
@@ -15,20 +15,27 @@
  * limitations under the License.
  */
 
-import { SimpleConsumer } from '..';
+import { Producer } from '..';
+import { topics, endpoints, sessionCredentials, namespace } from 
'./ProducerSingleton';
 
-const simpleConsumer = new SimpleConsumer({
-  consumerGroup: 'nodejs-demo-group',
-  endpoints: '127.0.0.1:8081',
-  namespace: '',
-  subscriptions: new Map().set('TopicTest', 'nodejs-demo'),
-});
-await simpleConsumer.startup();
+(async () => {
+  const producer = new Producer({
+    endpoints,
+    namespace,
+    sessionCredentials,
+    maxAttempts: 2,
+  });
+  await producer.startup();
+
+  const receipt = await producer.send({
+    topic: topics.fifo,
+    tag: 'nodejs-fifo',
+    body: Buffer.from(JSON.stringify({
+      hello: 'rocketmq-client-nodejs world 😄',
+      now: Date(),
+    })),
+    messageGroup: 'fifoMessageGroup',
+  });
+  console.log(receipt);
+})();
 
-const messages = await simpleConsumer.receive(20);
-console.log('got %d messages', messages.length);
-for (const message of messages) {
-  console.log(message);
-  console.log('body=%o', message.body.toString());
-  await simpleConsumer.ack(message);
-}
diff --git a/nodejs/examples/SimpleConsumer.ts 
b/nodejs/examples/ProducerNormalMessageExample.ts
similarity index 60%
copy from nodejs/examples/SimpleConsumer.ts
copy to nodejs/examples/ProducerNormalMessageExample.ts
index fb36c38d..c9d5a0dd 100644
--- a/nodejs/examples/SimpleConsumer.ts
+++ b/nodejs/examples/ProducerNormalMessageExample.ts
@@ -15,20 +15,26 @@
  * limitations under the License.
  */
 
-import { SimpleConsumer } from '..';
+import { Producer } from '..';
+import { topics, endpoints, sessionCredentials, namespace } from 
'./ProducerSingleton';
 
-const simpleConsumer = new SimpleConsumer({
-  consumerGroup: 'nodejs-demo-group',
-  endpoints: '127.0.0.1:8081',
-  namespace: '',
-  subscriptions: new Map().set('TopicTest', 'nodejs-demo'),
-});
-await simpleConsumer.startup();
 
-const messages = await simpleConsumer.receive(20);
-console.log('got %d messages', messages.length);
-for (const message of messages) {
-  console.log(message);
-  console.log('body=%o', message.body.toString());
-  await simpleConsumer.ack(message);
-}
+(async () => {
+  const producer = new Producer({
+    endpoints,
+    namespace,
+    sessionCredentials,
+    maxAttempts: 2,
+  });
+  await producer.startup();
+
+  const receipt = await producer.send({
+    topic: topics.normal,
+    tag: 'nodejs-normal',
+    body: Buffer.from(JSON.stringify({
+      hello: 'rocketmq-client-nodejs world 😄',
+      now: Date(),
+    })),
+  });
+  console.log(receipt);
+})();
diff --git a/nodejs/examples/ProducerSingleton.ts 
b/nodejs/examples/ProducerSingleton.ts
new file mode 100644
index 00000000..6775b559
--- /dev/null
+++ b/nodejs/examples/ProducerSingleton.ts
@@ -0,0 +1,38 @@
+/**
+ * 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 { SessionCredentials } from '../src/client';
+
+export const endpoints = process.env.ROCKETMQ_NODEJS_CLIENT_ENDPOINTS ?? 
'localhost:8081';
+export const namespace = process.env.ROCKETMQ_NODEJS_CLIENT_NAMESPACE ?? '';
+export const topics = {
+  normal: 'TopicTestForNormal',
+  fifo: 'TopicTestForFifo',
+  delay: 'TopicTestForDelay',
+  transaction: 'TopicTestForTransaction',
+};
+
+export const consumerGroup = process.env.ROCKETMQ_NODEJS_CLIENT_GROUP ?? 
'nodejs-unittest-group';
+export const tag = process.env.TAG ?? '*';
+
+export let sessionCredentials: SessionCredentials | undefined;
+if (process.env.ROCKETMQ_NODEJS_CLIENT_KEY && 
process.env.ROCKETMQ_NODEJS_CLIENT_SECRET) {
+  sessionCredentials = {
+    accessKey: process.env.ROCKETMQ_NODEJS_CLIENT_KEY,
+    accessSecret: process.env.ROCKETMQ_NODEJS_CLIENT_SECRET,
+  };
+}
diff --git a/nodejs/examples/ProducerTransactionMessageExample.ts 
b/nodejs/examples/ProducerTransactionMessageExample.ts
new file mode 100644
index 00000000..c904083f
--- /dev/null
+++ b/nodejs/examples/ProducerTransactionMessageExample.ts
@@ -0,0 +1,49 @@
+/**
+ * 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 { Producer } from '..';
+import { topics, endpoints, sessionCredentials, namespace, tag } from 
'./ProducerSingleton';
+import pkg from 
'rocketmq-client-nodejs/proto/apache/rocketmq/v2/definition_pb.js';
+const { TransactionResolution } = pkg;
+
+
+(async () => {
+  const producer = new Producer({
+    endpoints,
+    namespace,
+    sessionCredentials,
+    maxAttempts: 2,
+    checker: {
+      async check(messageView) {
+        console.log(messageView);
+        return TransactionResolution.COMMIT;
+      },
+    },
+  });
+  await producer.startup();
+  const transaction = producer.beginTransaction();
+  const receipt = await producer.send({
+    topic: topics.transaction,
+    tag,
+    body: Buffer.from(JSON.stringify({
+      hello: 'rocketmq-client-nodejs world 😄',
+      now: Date(),
+    })),
+  }, transaction);
+  await transaction.commit();
+  console.log(receipt);
+})();
diff --git a/nodejs/examples/SimpleConsumer.ts 
b/nodejs/examples/SimpleConsumer.ts
index fb36c38d..e154fda7 100644
--- a/nodejs/examples/SimpleConsumer.ts
+++ b/nodejs/examples/SimpleConsumer.ts
@@ -17,18 +17,25 @@
 
 import { SimpleConsumer } from '..';
 
-const simpleConsumer = new SimpleConsumer({
-  consumerGroup: 'nodejs-demo-group',
-  endpoints: '127.0.0.1:8081',
-  namespace: '',
-  subscriptions: new Map().set('TopicTest', 'nodejs-demo'),
-});
-await simpleConsumer.startup();
+import { topics, endpoints, sessionCredentials, namespace, tag, consumerGroup 
} from './ProducerSingleton';
 
-const messages = await simpleConsumer.receive(20);
-console.log('got %d messages', messages.length);
-for (const message of messages) {
-  console.log(message);
-  console.log('body=%o', message.body.toString());
-  await simpleConsumer.ack(message);
-}
+
+(async () => {
+  const simpleConsumer = new SimpleConsumer({
+    consumerGroup,
+    endpoints,
+    namespace,
+    sessionCredentials,
+    subscriptions: new Map().set(topics.normal, tag),
+    awaitDuration: 3000,
+  });
+  await simpleConsumer.startup();
+
+  const messages = await simpleConsumer.receive(20);
+  console.log('got %d messages', messages.length);
+  for (const message of messages) {
+    console.log(message);
+    console.log('body=%o', message.body.toString());
+    await simpleConsumer.ack(message);
+  }
+})();
diff --git a/nodejs/scripts/build-grpc.sh b/nodejs/scripts/build-grpc.sh
index c68d6dd7..72fd8090 100755
--- a/nodejs/scripts/build-grpc.sh
+++ b/nodejs/scripts/build-grpc.sh
@@ -56,8 +56,23 @@ generateGrpc() {
 
 echo ""
 echo "Removing old Proto Files: ${PATH_PROTO_OUTPUT}"
-rm -rf $PATH_PROTO_OUTPUT
-mkdir -p $PATH_PROTO_OUTPUT
+
+TARGET_DIR=$PATH_PROTO_OUTPUT
+
+if [ -z "$TARGET_DIR" ]; then
+  echo "Usage: $0 <directory>"
+  exit 1
+fi
+
+if [ ! -d "$TARGET_DIR" ]; then
+  echo "Error: Directory $TARGET_DIR does not exist."
+  exit 1
+fi
+
+echo "Removing .ts and .js files in $TARGET_DIR"
+find "$TARGET_DIR" -type f \( -name "*.ts" -o -name "*.js" \) -exec rm -f {} \;
+
+echo "DONE"
 
 echo ""
 echo "Compiling gRPC files"

Reply via email to