From f5cf988421fa0201938bd514111ed53c834e5e92 Mon Sep 17 00:00:00 2001 From: narawat Date: Thu, 4 Jun 2026 13:07:42 +0700 Subject: [PATCH] update --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index ead8efb..5dda2f3 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,50 @@ export async function searchWines(keyword, config) { } ``` +### Example 3: Basic Publish/Subscribe (No Msghandler) + +```javascript +import { getNats, subscribe, publish } from './natsClient.js'; + +async function basicPubSubExample() { + // Connect to NATS + await getNats(); + + // Subscribe to a topic + const subscription = await subscribe('my.topic', (message) => { + console.log('Received:', message); + }); + + // Publish a message + await publish('my.topic', { data: 'hello world' }); + + // Later, unsubscribe + await subscription.unsubscribe(); +} +``` + +### Example 4: Direct Request/Reply (No Msghandler) + +```javascript +import { getNats, request } from './natsClient.js'; + +async function directRequestExample() { + // Connect to NATS + await getNats(); + + // Send a direct request (no msghandler envelope) + const response = await request( + 'sommpanion.backend.dbapi.v1.inbox', + JSON.stringify({ + cmd: 'status', + data: {} + }) + ); + + console.log('Response:', response); +} +``` + ## Error Handling ### Connection Errors