mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
Clearer language, added java script example, fixed values in examples
Signed-off-by: Matthias Hanel <mh@synadia.com>
This commit is contained in:
parent
e7d70aaea7
commit
232c52a3f1
@ -1,6 +1,6 @@
|
||||
# Setting a Connect Timeout
|
||||
|
||||
Each library has its own, language preferred way, to pass connection options. One of the most common options is a connection timeout. To set the maximum time to connect to a server to 10 seconds:
|
||||
Each library has its own, language preferred way, to pass connection options. One of the most common options is a connect timeout. It limits how long it can take to establishe a connection to a server. Should multiple urls be provided, this timeout applies to each cluster member individually. To set the maximum time to connect to a server to 10 seconds:
|
||||
|
||||
{% tabs %}
|
||||
{% tab title="Go" %}
|
||||
@ -30,13 +30,29 @@ nc.close();
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="JavaScript" %}
|
||||
|
||||
```javascript
|
||||
let nc = NATS.connect({
|
||||
url: "nats://demo.nats.io:4222",
|
||||
timeout: 10*1000 //10s
|
||||
});
|
||||
nc.on('connect', (c) => {
|
||||
// Do something with the connection
|
||||
doSomething();
|
||||
// When done close it
|
||||
nc.close();
|
||||
});
|
||||
nc.on('error', (err) => {
|
||||
failed(err);
|
||||
});
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="Python" %}
|
||||
```python
|
||||
nc = NATS()
|
||||
await nc.connect(connect_timeout=2)
|
||||
await nc.connect(
|
||||
servers=["nats://demo.nats.io:4222"],
|
||||
connect_timeout=10)
|
||||
|
||||
# Do something with the connection
|
||||
|
||||
@ -49,7 +65,7 @@ await nc.close()
|
||||
# There is currently no connect timeout as part of the Ruby NATS client API, but you can use a timer to mimic it.
|
||||
require 'nats/client'
|
||||
|
||||
timer = EM.add_timer(5) do
|
||||
timer = EM.add_timer(10) do
|
||||
NATS.connect do |nc|
|
||||
# Do something with the connection
|
||||
|
||||
@ -65,7 +81,7 @@ EM.cancel_timer(timer)
|
||||
```typescript
|
||||
let nc = await connect({
|
||||
url: "nats://demo.nats.io:4222",
|
||||
timeout: 1000
|
||||
timeout: 10*1000 //10s
|
||||
});
|
||||
```
|
||||
{% endtab %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user