This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection_samples.js
72 lines (66 loc) · 1.58 KB
/
connection_samples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import test from "ava";
const { NatsServer } = require("./helpers/launcher");
import { connect, createInbox } from "nats";
const conf = {
authorization: {
token: "aToK3n",
PERM: {
subscribe: "bar",
publish: "foo",
},
users: [{
user: "byname",
password: "password",
permission: "$PERM",
}, {
user: "withnkey",
nkey: "UD6OU4D3CIOGIDZVL4ANXU3NWXOW5DCDE2YPZDBHPBXCVKHSODUA4FKI",
}],
},
};
function doSomething() {}
test("connect_default", async (t) => {
const ns = await NatsServer.start({ port: 4222 });
// [begin connect_default]
const nc = await connect();
// Do something with the connection
doSomething();
// When done close it
await nc.close();
// [end connect_default]
t.pass();
await ns.stop();
});
test("connect_url", async (t) => {
// [begin connect_url]
const nc = await connect({ servers: "demo.nats.io" });
// Do something with the connection
doSomething();
await nc.close();
// [end connect_url
t.pass();
});
test("connect_multiple", async (t) => {
// [begin connect_multiple]
const nc = await connect({
servers: [
"nats://demo.nats.io:4222",
"nats://localhost:4222",
],
});
// Do something with the connection
doSomething();
// When done close it
await nc.close();
// [end connect_multiple]
t.pass();
});
test("drain_conn", async (t) => {
// [begin drain_conn]
const nc = await connect({ servers: "demo.nats.io" });
const sub = nc.subscribe(createInbox(), () => {});
nc.publish(sub.getSubject());
await nc.drain();
// [end drain_conn]
t.pass();
});