Skip to content

Commit ec54eeb

Browse files
Jaremaclaude
andcommitted
Add getting-started publish and subscribe examples
Add documentation examples for the getting-started guide: - Publish example showing basic message publishing - Subscribe example showing basic subscription handling These examples use NATS-DOC-START/END markers for extraction to the documentation site. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent ea522a8 commit ec54eeb

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { connect, StringCodec } from '@nats-io/transport-node';
2+
3+
// Connect to NATS
4+
const nc = await connect({ servers: 'localhost:4222' });
5+
console.log('Connected to NATS');
6+
7+
// Create encoder
8+
const sc = StringCodec();
9+
10+
// NATS-DOC-START
11+
// Publish messages
12+
nc.publish('hello', sc.encode('Hello NATS!'));
13+
nc.publish('hello', sc.encode('Welcome to messaging'));
14+
// NATS-DOC-END
15+
16+
console.log('Messages published');
17+
18+
// Close connection
19+
await nc.close();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { connect, StringCodec } from '@nats-io/transport-node';
2+
3+
// Connect to NATS
4+
const nc = await connect({ servers: 'localhost:4222' });
5+
console.log('Connected to NATS');
6+
7+
// Create decoder
8+
const sc = StringCodec();
9+
10+
// NATS-DOC-START
11+
// Subscribe to 'hello'
12+
const sub = nc.subscribe('hello');
13+
console.log('Waiting for messages...');
14+
15+
// Process messages
16+
for await (const msg of sub) {
17+
console.log(`Received: ${sc.decode(msg.data)}`);
18+
}
19+
// NATS-DOC-END

0 commit comments

Comments
 (0)