mirror of
				https://github.com/taigrr/nats.docs
				synced 2025-01-18 04:03:23 -08:00 
			
		
		
		
	
		
			
				
	
	
		
			170 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			170 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| 
 | |
| <div class="tab-wrap">
 | |
|   	
 | |
| 	
 | |
| 	<input type="radio" id="unsubscribe_go" name="unsubscribe" class="tab" checked>
 | |
| 	
 | |
| 	<label for="unsubscribe_go" class="api-lang" data-language="go">Go</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="unsubscribe_java" name="unsubscribe" class="tab">
 | |
| 	
 | |
| 	<label for="unsubscribe_java" class="api-lang" data-language="java">Java</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="unsubscribe_js" name="unsubscribe" class="tab">
 | |
| 	
 | |
| 	<label for="unsubscribe_js" class="api-lang" data-language="js">JavaScript</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="unsubscribe_py" name="unsubscribe" class="tab">
 | |
| 	
 | |
| 	<label for="unsubscribe_py" class="api-lang" data-language="py">Python</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="unsubscribe_ruby" name="unsubscribe" class="tab">
 | |
| 	
 | |
| 	<label for="unsubscribe_ruby" class="api-lang" data-language="ruby">Ruby</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="unsubscribe_ts" name="unsubscribe" class="tab">
 | |
| 	
 | |
| 	<label for="unsubscribe_ts" class="api-lang" data-language="ts">TypeScript</label>
 | |
| 	
 | |
| 
 | |
|   	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="unsubscribe_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/unsubscribe/main.go#L10-35"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-go">nc, err := nats.Connect("demo.nats.io")
 | |
| if err != nil {
 | |
| 	log.Fatal(err)
 | |
| }
 | |
| defer nc.Close()
 | |
| 
 | |
| // Sync Subscription
 | |
| sub, err := nc.SubscribeSync("updates")
 | |
| if err != nil {
 | |
| 	log.Fatal(err)
 | |
| }
 | |
| if err := sub.Unsubscribe(); err != nil {
 | |
| 	log.Fatal(err)
 | |
| }
 | |
| 
 | |
| // Async Subscription
 | |
| sub, err = nc.Subscribe("updates", func(_ *nats.Msg) {})
 | |
| if err != nil {
 | |
| 	log.Fatal(err)
 | |
| }
 | |
| if err := sub.Unsubscribe(); err != nil {
 | |
| 	log.Fatal(err)
 | |
| }
 | |
| 
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="unsubscribe_java_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/java-nats-examples/blob/master/src/main/java/io/nats/examples/Unsubscribe.java#L14-31"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-java">Connection nc = Nats.connect("nats://demo.nats.io:4222");
 | |
| Dispatcher d = nc.createDispatcher((msg) -> {
 | |
|     String str = new String(msg.getData(), StandardCharsets.UTF_8);
 | |
|     System.out.println(str);
 | |
| });
 | |
| 
 | |
| // Sync Subscription
 | |
| Subscription sub = nc.subscribe("updates");
 | |
| sub.unsubscribe();
 | |
| 
 | |
| // Async Subscription
 | |
| d.subscribe("updates");
 | |
| d.unsubscribe("updates");
 | |
| 
 | |
| // Close the connection
 | |
| nc.close();
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="unsubscribe_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/subscriber_samples.js#L51-65"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">let nc = NATS.connect({
 | |
|     url: "nats://demo.nats.io:4222"
 | |
| });
 | |
| // set up a subscription to process a request
 | |
| let sub = nc.subscribe(NATS.createInbox(), (msg, reply) => {
 | |
|     if (msg.reply) {
 | |
|         nc.publish(reply, new Date().toLocaleTimeString());
 | |
|     }
 | |
| });
 | |
| 
 | |
| // without arguments the subscription will cancel when the server receives it
 | |
| // you can also specify how many messages are expected by the subscription
 | |
| nc.unsubscribe(sub);
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="unsubscribe_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/unsubscribe.py#L6-26"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-python">nc = NATS()
 | |
| 
 | |
| await nc.connect(servers=["nats://demo.nats.io:4222"])
 | |
| 
 | |
| future = asyncio.Future()
 | |
| 
 | |
| async def cb(msg):
 | |
|   nonlocal future
 | |
|   future.set_result(msg)
 | |
| 
 | |
| sid = await nc.subscribe("updates", cb=cb)
 | |
| await nc.publish("updates", b'All is Well')
 | |
| 
 | |
| # Remove interest in subject
 | |
| await nc.unsubscribe(sid)
 | |
| 
 | |
| # Won't be received...
 | |
| await nc.publish("updates", b'...')
 | |
| 
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="unsubscribe_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/unsubscribe.rb#L1-27"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-ruby">require 'nats/client'
 | |
| require 'fiber'
 | |
| 
 | |
| NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
 | |
|   Fiber.new do
 | |
|     f = Fiber.current
 | |
| 
 | |
|     sid = nc.subscribe("time") do |msg, reply|
 | |
|       f.resume Time.now
 | |
|     end
 | |
| 
 | |
|     nc.publish("time", 'What is the time?', NATS.create_inbox)
 | |
| 
 | |
|     # Use the response
 | |
|     msg = Fiber.yield
 | |
|     puts "Reply: #{msg}"
 | |
| 
 | |
|     nc.unsubscribe(sid)
 | |
| 
 | |
|     # Won't be received
 | |
|     nc.publish("time", 'What is the time?', NATS.create_inbox)
 | |
| 
 | |
|   end.resume
 | |
| end
 | |
| 
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="unsubscribe_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/subscriber_samples.ts#L51-64"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">// set up a subscription to process a request
 | |
| let sub = await nc.subscribe(createInbox(), (err, msg) => {
 | |
|     if (msg.reply) {
 | |
|         nc.publish(msg.reply, new Date().toLocaleTimeString());
 | |
|     } else {
 | |
|         t.log('got a request for the time, but no reply subject was set.');
 | |
|     }
 | |
| });
 | |
| 
 | |
| // without arguments the subscription will cancel when the server receives it
 | |
| // you can also specify how many messages are expected by the subscription
 | |
| sub.unsubscribe();
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| </div>
 |