PageSourceSearch

https://natsbyexample.com/examples/messaging/pub-sub/cli/

html natsbyexample.com collected 2026-10-02 06:28:12 UTC 11,339 bytes, 334 lines download raw bytes

1<!doctype html>
2<html>
3<head>
4	<title>NATS by Example - Core Publish-Subscribe (CLI)</title>
5<meta charset="utf-8" />
6<meta name="viewport" content="width=device-width, initial-scale=1" />
7<link rel="icon" href="/nats.svg" />
8<link rel="stylesheet" href="/reset.css">
9<link rel="stylesheet" href="/main.css">
vendor: 66 bytes, lines 9-12
9
10
11
12<script async src="https://www.googletagmanager.com/gtag/js?id=
12G-6242VH03CH
vendor: 12 bytes, line 12
12"></script>
13<script>
14  
vendor: 134 bytes, lines 14-17
14window.dataLayer = window.dataLayer || [];
15  function gtag() {dataLayer.push(arguments);}
16  gtag('js', new Date());
17  gtag('config', '
17G-6242VH03CH
vendor: 4 bytes, line 17
17');
18</script>
18
19
20  <meta name="twitter:card" content="summary" />
21  <meta name="twitter:creator" content="@thedevel" />
22  <meta name="twitter:title" content="Core Publish-Subscribe (CLI)" />
23  <meta name="twitter:description" content="This example demonstrates the core NATS publish-subscribe
24behavior. This is the fundamental pattern that all other NATS
25patterns and higher-level APIs build upon. There are a few takeaways from this example:
26
27
28Delivery is an at-most-once. For MQTT users, this is referred to as Quality of Service (QoS) 0.
29There are two circumstances when a published message won&rsquo;t be delivered to a subscriber:
30
31
32The subscriber does not have an active connection to the server (i.e. the client is temporarily offline for some reason)
33There is a network interruption where the message is ultimately dropped
34
35Messages are published to subjects which can be one or more concrete tokens, e.g. greet.bob. Subscribers can utilize wildcards to show interest on a set of matching subjects.
36
37" />
38  <meta name="twitter:image" content="https://natsbyexample.com/nbe-twitter.png" />
39  <meta name="twitter:image:alt" content="NATS by Example" />
40
41  <link rel="stylesheet" type="text/css" href="/asciinema-player.css" />
42</head>
43<body>
44  <header>
45    <div class="container">
46      <h1 id="header">
47  <a href="/">
48    <img src="/nats-horizontal-color.svg" alt="NATS Logo" /> by Example
49  </a>
50</h1>
51
52    </div>
53  </header>
54
55  <main>
56    <div class="container">
57      <h2 class="title">Core Publish-Subscribe <small class="quiet">in Messaging</small></h2>
58
59      <div class="description">
60      <p>This example demonstrates the core NATS <a href="https://docs.nats.io/nats-concepts/core-nats/pubsub">publish-subscribe</a>
61behavior. This is the fundamental pattern that all other NATS
62patterns and higher-level APIs build upon. There are a few takeaways from this example:</p>
63
64<ul>
65<li>Delivery is an <em>at-most-once</em>. For <a href="https://docs.nats.io/running-a-nats-service/configuration/mqtt">MQTT</a> users, this is referred to as Quality of Service (QoS) 0.</li>
66<li>There are two circumstances when a published message <em>won&rsquo;t</em> be delivered to a subscriber:
67
68<ul>
69<li>The subscriber does not have an active connection to the server (i.e. the client is temporarily offline for some reason)</li>
70<li>There is a network interruption where the message is ultimately dropped</li>
71</ul></li>
72<li>Messages are published to <a href="https://docs.nats.io/nats-concepts/subjects">subjects</a> which can be one or more concrete tokens, e.g. <code>greet.bob</code>. Subscribers can utilize <a href="https://docs.nats.io/nats-concepts/subjects#wildcards">wildcards</a> to show interest on a set of matching subjects.</li>
73</ul>
74
75      </div>
76
77      
78
79      <div class="info">
80        
81
82        <div class="language-tabs">
83          
84          
85          <span class="active">CLI</span>
86          
87          
88          
89          <span><a href="/examples/messaging/pub-sub/go">Go</a></span>
90          
91          
92          
93          <span><a href="/examples/messaging/pub-sub/python">Python</a></span>
94          
95          
96          
97          <span><a href="/examples/messaging/pub-sub/deno">JavaScript</a></span>
98          
99          
100          
101          <span><a href="/examples/messaging/pub-sub/rust">Rust</a></span>
102          
103          
104          
105          <span><a href="/examples/messaging/pub-sub/csharp">C#</a></span>
106          
107          
108          
109          <span><a href="/examples/messaging/pub-sub/java">Java</a></span>
110          
111          
112          
113          <span class="inactive" title="Not yet implemented">Ruby</span>
114          
115          
116          
117          <span><a href="/examples/messaging/pub-sub/elixir">Elixir</a></span>
118          
119          
120          
121          <span><a href="/examples/messaging/pub-sub/crystal">Crystal</a></span>
122          
123          
124          
125          <span class="inactive" title="Not yet implemented">C</span>
126          
127          
128          <div><small>Jump to the <a href="#output">output</a> or the <a href="#recording">recording</a></small></div>
129        </div>
130
131        <div class="source-run">
132          <pre>$ nbe run messaging/pub-sub/cli</pre>
133          <small>
134            View the <a href="https://github.com/ConnectEverything/nats-by-example/tree/main/examples/messaging/pub-sub/cli" target=_blank>source code</a> or
135            <a target=_blank href="https://github.com/ConnectEverything/nats-by-example/#getting-started">learn</a> how to run this example yourself</small>
136        </div>
137
138      </div>
139
140      <h3 id="code">Code</h3>
141
142      <div class="example">
143      
144      
145        <div class="example-comment">
146        
147        </div>
148      
149      
150      
151        <div class="example-code">
152        <pre class="chroma"><code><span class="line"><span class="cl"><span class="cp">#!/bin/sh
153</span></span></span><span class="line"><span class="cl">
154</span></span></code></pre>
155        </div>
156      
157      
158      
159        <div class="example-comment">
160        <p>The <code>nats</code> CLI utilizes the <code>NATS_URL</code> environment variable if set.
161However, if you want to manage different <em>contexts</em>
161 for connecting
162or authenticating, check out the <code>nats context</code> commands.
163For example:</p>
164
165<pre><code>nats context save --server=$NATS_URL local
166</code></pre>
167
168<p>Start a nats-server in the background.</p>
169
170        </div>
171      
172      
173      
174        <div class="example-code">
175        <pre class="chroma"><code><span class="line"><span class="cl">nats-server <span class="p">&amp;</span>
176</span></span><span class="line"><span class="cl">
177</span></span><span class="line"><span class="cl">
178</span></span><span class="line"><span class="cl"><span class="nv">NATS_URL</span><span class="o">=</span><span class="s2">&#34;</span><span class="si">${</span><span class="nv">NATS_URL</span><span class="k">:-</span><span class="nv">nats</span><span class="p">://localhost:</span><span class="nv">4222</span><span class="si">}</span><span class="s2">&#34;</span>
179</span></span><span class="line"><span class="cl">
180</span></span></code></pre>
181        </div>
182      
183      
184      
185        <div class="example-comment">
186        <p>Publish a message to the subject &lsquo;greet.joe&rsquo;. Nothing will happen
187since the subscription is not yet setup.</p>
188
189        </div>
190      
191      
192      
193        <div class="example-code">
194        <pre class="chroma"><code><span class="line"><span class="cl">nats pub <span class="s1">&#39;greet.joe&#39;</span> <span class="s1">&#39;hello&#39;</span>
195</span></span><span class="line"><span class="cl">
196</span></span></code></pre>
197        </div>
198      
199      
200      
201        <div class="example-comment">
202        <p>Let&rsquo;s start a subscription in the background that will print
203the output to stdout.</p>
204
205        </div>
206      
207      
208      
209        <div class="example-code">
210        <pre class="chroma"><code><span class="line"><span class="cl">nats sub <span class="s1">&#39;greet.*&#39;</span> <span class="p">&amp;</span>
211</span></span><span class="line"><span class="cl">
212</span></span></code></pre>
213        </div>
214      
215      
216      
217        <div class="example-comment">
218        <p>This just captures the process ID of the previous command in this shell.</p>
219
220        </div>
221      
222      
223      
224        <div class="example-code">
225        <pre class="chroma"><code><span class="line"><span class="cl"><span class="nv">SUB_PID</span><span class="o">=</span><span class="nv">$!</span>
226</span></span><span class="line"><span class="cl">
227</span></span></code></pre>
228        </div>
229      
230      
231      
232        <div class="example-comment">
233        <p>Tiny sleep to ensure the subscription connected.</p>
234
235        </div>
236      
237      
238      
239        <div class="example-code">
240        <pre class="chroma"><code><span class="line"><span class="cl">sleep 0.5
241</span></span><span class="line"><span class="cl">
242</span></span></code></pre>
243        </div>
244      
245      
246      
247        <div class="example-comment">
248        <p>Now we can publish a couple times..</p>
249
250        </div>
251      
252      
253      
254        <div class="example-code">
255        <pre class="chroma"><code><span class="line"><span class="cl">nats pub <span class="s1">&#39;greet.joe&#39;</span> <span class="s1">&#39;hello&#39;</span>
256</span></span><span class="line"><span class="cl">nats pub <span class="s1">&#39;greet.pam&#39;</span> <span class="s1">&#39;hello&#39;</span>
257</span></span><span class="line"><span class="cl">nats pub <span class="s1">&#39;greet.bob&#39;</span> <span class="s1">&#39;hello&#39;</span>
258</span></span><span class="line"><span class="cl">
259</span></span></code></pre>
260        </div>
261      
262      
263      
264        <div class="example-comment">
265        <p>Remove the subscription.</p>
266
267        </div>
268      
269      
270      
271        <div class="example-code">
272        <pre class="chroma"><code><span class="line"><span class="cl"><span class="nb">kill</span> <span class="nv">$SUB_PID</span>
273</span></span><span class="line"><span class="cl">
274</span></span></code></pre>
275        </div>
276      
277      
278      
279        <div class="example-comment">
280        <p>Publishing again will not result in anything.</p>
281
282        </div>
283      
284      
285      
286        <div class="example-code">
287        <pre class="chroma"><code><span class="line"><span class="cl">nats pub <span class="s1">&#39;greet.bob&#39;</span> <span class="s1">&#39;hello&#39;</span></span></span></code></pre>
288        </div>
289      
290      
291      </div>
292
293      <h3 id="output">Output</h3>
294      <pre class="output">13:57:13 Published 5 bytes to &#34;greet.joe&#34;
29513:57:13 Subscribing on greet.* 
29613:57:13 Published 5 bytes to &#34;greet.joe&#34;
297[#1] Received on &#34;greet.joe&#34;
298hello
299
30013:57:13 Published 5 bytes to &#34;greet.pam&#34;
301[#2] Received on &#34;greet.pam&#34;
302hello
303
30413:57:13 Published 5 bytes to &#34;greet.bob&#34;
305[#3] Received on &#34;greet.bob&#34;
306hello
307
30813:57:13 Published 5 bytes to &#34;greet.bob&#34;
309</pre>
310
311      <h3 id="recording">Recording</h3>
312      <div class="quiet text-center">Note, playback is half speed to make it a bit easier to follow.</div>
313      <div id="asciinema"></div>
314
315    </div>
316  </main>
317  
317<script src="/main.js" async></script>
317
318  
318<script src="/asciinema-player.min.js"></script>
318
319  
319<script>
320    var url = '/examples\/messaging\/pub-sub\/cli\/output.cast';
321    AsciinemaPlayer.create(url, document.getElementById('asciinema'), {
322      cols: 120,
323      rows: 24,
324      fit: false,
325      speed: 0.5,
326      terminalFontSize: "14px",
327      terminalFontFamily: "'Roboto Mono', 'JetBrains Mono', 'Source Code Pro', 'FreeMono', monospace",
328      terminalFontHeight: 1.4,
329    });
330  </script>
330
331  <footer>
332  </footer>
333</body>
334</html>

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.