mirror of
				https://github.com/taigrr/nats.docs
				synced 2025-01-18 04:03:23 -08:00 
			
		
		
		
	
		
			
				
	
	
		
			97 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| 
 | |
| <div class="tab-wrap">
 | |
|   	
 | |
| 	
 | |
| 	<input type="radio" id="connect_creds_go" name="connect_creds" class="tab" checked>
 | |
| 	
 | |
| 	<label for="connect_creds_go" class="api-lang" data-language="go">Go</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="connect_creds_java" name="connect_creds" class="tab">
 | |
| 	
 | |
| 	<label for="connect_creds_java" class="api-lang" data-language="java">Java</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="connect_creds_js" name="connect_creds" class="tab">
 | |
| 	
 | |
| 	<label for="connect_creds_js" class="api-lang" data-language="js">JavaScript</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="connect_creds_py" name="connect_creds" class="tab">
 | |
| 	
 | |
| 	<label for="connect_creds_py" class="api-lang" data-language="py">Python</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="connect_creds_ts" name="connect_creds" class="tab">
 | |
| 	
 | |
| 	<label for="connect_creds_ts" class="api-lang" data-language="ts">TypeScript</label>
 | |
| 	
 | |
| 
 | |
|   	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="connect_creds_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/connect_creds/main.go#L10-19"><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("127.0.0.1", nats.UserCredentials("path_to_creds_file"))
 | |
| if err != nil {
 | |
| 	log.Fatal(err)
 | |
| }
 | |
| defer nc.Close()
 | |
| 
 | |
| // Do something with the connection
 | |
| 
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="connect_creds_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/ConnectCreds.java#L11-21"><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">Options options = new Options.Builder().
 | |
|             server("nats://localhost:4222").
 | |
|             authHandler(Nats.credentials("path_to_creds_file")).
 | |
|             build();
 | |
| Connection nc = Nats.connect(options);
 | |
| 
 | |
| // Do something with the connection
 | |
| 
 | |
| nc.close();
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="connect_creds_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/auth_examples.js#L192-200"><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">// credentials file contains the JWT and the secret signing key
 | |
| let credsFile = path.join(confDir, 'credsfile.creds');
 | |
| 
 | |
| let nc = NATS.connect({
 | |
|     url: server.nats,
 | |
|     userCreds: credsFile
 | |
| });
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="connect_creds_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/connect_creds.py#L5-20"><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()
 | |
| 
 | |
| async def error_cb(e):
 | |
|     print("Error:", e)
 | |
| 
 | |
| await nc.connect("nats://localhost:4222",
 | |
|                  user_credentials="path_to_creds_file",
 | |
|                  error_cb=error_cb,
 | |
|                  )
 | |
| 
 | |
| # Do something with the connection
 | |
| 
 | |
| await nc.close()
 | |
| 
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="connect_creds_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/auth_examples.ts#L160-168"><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">// credentials file contains the JWT and the secret signing key
 | |
| let credsFile = path.join(confDir, 'credsfile.creds');
 | |
| 
 | |
| let nc = await connect({
 | |
|     url: server.nats,
 | |
|     userCreds: credsFile
 | |
| });
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| </div>
 |