Turning Off Echo'd Messages
By default the server will echo messages. This means that if a publisher on a connection sends a message to a subject any subscribers on that same connection will receive the message. Turning off echo is a fairly new feature for the NATS server, but some of the clients already support it.
digraph {
rankdir=LR;
subgraph cluster_1 {
shape=box;
style="rounded";
label = "Connection #1";
publisher [shape=box, style="rounded", label="Publisher"];
subscriber_1 [shape=box, style="rounded", label="Subscriber"];
}
subgraph cluster_2 {
shape=box;
style="rounded";
label = "Connection #2";
subscriber_2 [shape=box, style="rounded", label="Subscriber"];
}
subject [shape=circle, label="Subject"];
publisher -> subject [label="msg"];
subject -> subscriber_1 [label="echo'd msg", style="dashed"];
subject -> subscriber_2 [label="msg"];
}
Keep in mind that each connection will have to turn off echo, and that it is per connection, not per application. Also, turning echo on and off can result in a major change to your applications communications protocol since messages will flow or stop flowing based on this setting and the subscribing code won't have any indication as to why.
// Turn off echo
nc, err := nats.Connect("demo.nats.io", nats.NoEcho())
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Do something with the connection
Options options = new Options.Builder().
server("nats://demo.nats.io:4222").
noEcho(). // Turn off echo
build();
Connection nc = Nats.connect(options);
// Do something with the connection
nc.close();
ncA = NATS()
ncB = NATS()
await ncA.connect(no_echo=True)
await ncB.connect()
async def handler(msg):
# Messages sent by `ncA' will not be received.
print("[Received] ", msg)
await ncA.subscribe("greetings", cb=handler)
await ncA.flush()
await ncA.publish("greetings", b'Hello World!')
await ncB.publish("greetings", b'Hello World!')
# Do something with the connection
await asyncio.sleep(1)
await ncA.drain()
await ncB.drain()