How can I implement NATS MESSAGE-ID? #1439
-
output
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hi, as far as I understand you, you want to send message with deduplication header, it is called await broker.publish(message_body, headers={"Nats-Msg-Id": "your_deduplication_key"}) |
Beta Was this translation helpful? Give feedback.
-
is it jet stream, I'm I right? |
Beta Was this translation helpful? Give feedback.
-
By default FastStream are use Nats-Core instead of Nats-Jetstream, for enable JS you must pass import json
import uuid
from faststream import FastStream, Logger
from faststream.nats import NatsBroker, JStream, NatsMessage
broker = NatsBroker()
app = FastStream(broker)
CHANNEL_NAME = "telegram.billing"
publisher = broker.publisher(CHANNEL_NAME)
@broker.subscriber(CHANNEL_NAME, stream=JStream(name="stream_name"), durable="durable_name")
async def handler(msg: NatsMessage, logger: Logger):
"""
handler.
"""
logger.info(msg)
await msg.ack()
@app.after_startup
async def test_send():
"""
test send.
"""
msg = {
"channel_type": "billing",
"message": "Test message"
}
msg = json.dumps(msg)
deduplication_key = str(uuid.uuid4())
for _ in range(0, 2):
await publisher.publish(msg, headers={"Nats-Msg-Id": deduplication_key}) |
Beta Was this translation helpful? Give feedback.
By default FastStream are use Nats-Core instead of Nats-Jetstream, for enable JS you must pass
stream
parameter to your subscriber.Minimal reproducible example: