Powered By GitBook
Unsubscribing
The client libraries provide a means to unsubscribe a previous subscription request.
This process requires an interaction with the server, so for an asynchronous subscription there may be a small window of time where a message comes through as the unsubscribe is processed by the library. Ignoring that slight edge case, the client library will clean up any outstanding messages and tell the server that the subscription is no longer used.
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
nc, err := nats.Connect("demo.nats.io")
2
if err != nil {
3
log.Fatal(err)
4
}
5
defer nc.Close()
6
7
// Sync Subscription
8
sub, err := nc.SubscribeSync("updates")
9
if err != nil {
10
log.Fatal(err)
11
}
12
if err := sub.Unsubscribe(); err != nil {
13
log.Fatal(err)
14
}
15
16
// Async Subscription
17
sub, err = nc.Subscribe("updates", func(_ *nats.Msg) {})
18
if err != nil {
19
log.Fatal(err)
20
}
21
if err := sub.Unsubscribe(); err != nil {
22
log.Fatal(err)
23
}
Copied!
1
Connection nc = Nats.connect("nats://demo.nats.io:4222");
2
Dispatcher d = nc.createDispatcher((msg) -> {
3
String str = new String(msg.getData(), StandardCharsets.UTF_8);
4
System.out.println(str);
5
});
6
7
// Sync Subscription
8
Subscription sub = nc.subscribe("updates");
9
sub.unsubscribe();
10
11
// Async Subscription
12
d.subscribe("updates");
13
d.unsubscribe("updates");
14
15
// Close the connection
16
nc.close();
Copied!
1
let nc = NATS.connect({
2
url: "nats://demo.nats.io:4222"
3
});
4
// set up a subscription to process a request
5
let sub = nc.subscribe(NATS.createInbox(), (msg, reply) => {
6
if (msg.reply) {
7
nc.publish(reply, new Date().toLocaleTimeString());
8
}
9
});
10
11
// without arguments the subscription will cancel when the server receives it
12
// you can also specify how many messages are expected by the subscription
13
nc.unsubscribe(sub);
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://demo.nats.io:4222"])
4
5
future = asyncio.Future()
6
7
async def cb(msg):
8
nonlocal future
9
future.set_result(msg)
10
11
sid = await nc.subscribe("updates", cb=cb)
12
await nc.publish("updates", b'All is Well')
13
14
# Remove interest in subject
15
await nc.unsubscribe(sid)
16
17
# Won't be received...
18
await nc.publish("updates", b'...')
Copied!
1
require 'nats/client'
2
require 'fiber'
3
4
NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
5
Fiber.new do
6
f = Fiber.current
7
8
sid = nc.subscribe("time") do |msg, reply|
9
f.resume Time.now
10
end
11
12
nc.publish("time", 'What is the time?', NATS.create_inbox)
13
14
# Use the response
15
msg = Fiber.yield
16
puts "Reply: #{msg}"
17
18
nc.unsubscribe(sid)
19
20
# Won't be received
21
nc.publish("time", 'What is the time?', NATS.create_inbox)
22
23
end.resume
24
end
Copied!
1
// set up a subscription to process a request
2
let sub = await nc.subscribe(createInbox(), (err, msg) => {
3
if (msg.reply) {
4
nc.publish(msg.reply, new Date().toLocaleTimeString());
5
} else {
6
t.log('got a request for the time, but no reply subject was set.');
7
}
8
});
9
10
// without arguments the subscription will cancel when the server receives it
11
// you can also specify how many messages are expected by the subscription
12
sub.unsubscribe();
Copied!
1
natsConnection *conn = NULL;
2
natsSubscription *sub = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
6
7
// Subscribe
8
if (s == NATS_OK)
9
s = natsConnection_SubscribeSync(&sub, conn, "updates");
10
11
// Unsubscribe
12
if (s == NATS_OK)
13
s = natsSubscription_Unsubscribe(sub);
14
15
(...)
16
17
// Destroy objects that were created
18
natsSubscription_Destroy(sub);
19
natsConnection_Destroy(conn);
Copied!
Last modified 1yr ago
Export as PDF
Copy link