Skip to content

Commit 2f1cc31

Browse files
update documentation
1 parent 3d2b6ed commit 2f1cc31

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

node/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ await channel.send({
7979
payload: 'Hello World'
8080
});
8181

82-
const reply = await channel.receive(); // You can still use receive() for single messages
83-
console.log('Reply:', reply.payload);
82+
// Reading messages (Async Iterator handles backpressure)
83+
for await (const msg of channel) {
84+
console.log('Reply:', msg.payload);
85+
if (msg.payload === 'Hello World') break; // Exit loop to close
86+
}
8487

8588
await channel.close();
8689
```
@@ -91,3 +94,13 @@ Remember to close the mesh connection when your application exits.
9194
```typescript
9295
await mesh.close();
9396
```
97+
98+
### 6. Async Iterators (Backpressure)
99+
Mesh supports Async Iterators (`for await...of`) for consuming messages. This is the **recommended** way to read from a channel as it automatically handles backpressure: the sender will be slowed down if your processing loop is slower than the incoming data rate.
100+
101+
```typescript
102+
for await (const msg of channel) {
103+
await processMessage(msg);
104+
// The next message is only requested after this line completes
105+
}
106+
```

python/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,19 @@ channel = await mesh.service("target-service-name").request_channel("session-id-
8888
out_msg = Message(function_name="greet", payload="Hello World")
8989
await channel.send(out_msg)
9090

91-
reply = await channel.receive()
92-
print(f"Reply: {reply.payload}")
91+
# Reading messages (Async Iterator handles backpressure)
92+
async for msg in channel:
93+
print(f"Reply: {msg.payload}")
94+
if msg.payload == "Hello World": break # Exit loop to close
9395

9496
await channel.close()
9597
```
98+
99+
### 5. Async Iterators (Backpressure)
100+
Mesh supports Async Iterators (`async for ... in`) for consuming messages. This is the **recommended** way to read from a channel as it automatically handles backpressure.
101+
102+
```python
103+
async for msg in channel:
104+
await process_message(msg)
105+
# The next message is consumed only after the loop body finishes
106+
```

0 commit comments

Comments
 (0)