Skip to content

Commit 05bfb7d

Browse files
apietroni51magne
andauthored
30 as a library user i want to see examples on how to use all the functionalities (#32)
* WIP: add examples * feat: add basic example in separate folder * chore: remove unused tsconfig --------- Co-authored-by: magne <[email protected]>
1 parent 835e204 commit 05bfb7d

File tree

9 files changed

+166
-41
lines changed

9 files changed

+166
-41
lines changed

README.md

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# RabbitMQ AMQP 1.0 JavaScript Client
22

33
This library is meant to be used with RabbitMQ 4.0. </br>
4-
Suitable for testing in pre-production environments. The public API(s) could change.
4+
Suitable for testing in pre-production environments. The public API(s) could change.
55

66
[![Build Status](https://github.com/coders51/rabbitmq-amqp-js-client/actions/workflows/main.yml/badge.svg)](https://github.com/coders51/rabbitmq-amqp-js-client/actions)
77

@@ -25,42 +25,7 @@ The client is distributed via **npm**:
2525

2626
## Getting started
2727

28-
**NOTE:** This is just a first example and will be replaced with a reference to the _examples folder_
29-
30-
The following example demonstrates how to create an environment, open a connection, and use the management to create and delete queues, exchanges, and bindings.
31-
32-
```typescript
33-
const environment = createEnvironment({
34-
host: "localhost",
35-
port: 5672,
36-
username: "rabbit",
37-
password: "rabbit",
38-
})
39-
40-
const connection = await environment.createConnection()
41-
42-
const management = connection.management()
43-
44-
const queue = await management.declareQueue("test")
45-
46-
const exchange = await management.declareExchange("exchange", { type: "topic" })
47-
const secondExchange = await management.declareExchange("exchange-dest", { type: "topic" })
48-
49-
const bindingToQueue = await management.bind("foo", { source: exchange, destination: queue })
50-
const bindingToExchange = await management.bind("foo", { source: exchange, destination: secondExchange })
51-
52-
await management.unbind("foo", { source: exchange, destination: queue })
53-
await management.unbind("foo", { source: exchange, destination: secondExchange })
54-
55-
await management.deleteExchange("exchange")
56-
await management.deleteExchange("exchange-dest")
57-
await management.deleteQueue("test")
58-
59-
management.close()
60-
await connection.close()
61-
await environment.close()
62-
```
63-
28+
Inside the [_examples_](./examples/) folder you can find a node project that shows how to use the library.
6429

6530
## Resources
6631

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Client examples
2+
3+
- [Basic Usage](./index.js) - Producer and Consumer example without reconnection

examples/index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const rabbit = require("rabbitmq-amqp-js-client")
2+
const { randomUUID } = require("crypto")
3+
4+
const rabbitUser = process.env.RABBITMQ_USER || "rabbit"
5+
const rabbitPassword = process.env.RABBITMQ_PASSWORD || "rabbit"
6+
7+
async function main() {
8+
const testExchange = `test-exchange-${randomUUID()}`
9+
const testQueue = `test-queue-${randomUUID()}`
10+
const routingKey = `test-key-${randomUUID()}`
11+
12+
console.log("Creating the environment...")
13+
const environment = rabbit.createEnvironment({
14+
host: "localhost",
15+
port: 5672,
16+
username: rabbitUser,
17+
password: rabbitPassword,
18+
})
19+
20+
console.log("Opening a connection...")
21+
const connection = await environment.createConnection()
22+
const management = connection.management()
23+
24+
console.log("Creating a queue and an exchange...")
25+
const queue = await management.declareQueue(testQueue)
26+
const exchange = await management.declareExchange(testExchange)
27+
28+
console.log("Binding exchange to queue...")
29+
await management.bind(routingKey, { source: exchange, destination: queue })
30+
31+
console.log("Opening a publisher and publishing 10 messages...")
32+
const publisher = await connection.createPublisher({ exchange: { name: testExchange, routingKey: routingKey } })
33+
for (const i of Array(10).keys()) {
34+
const publishResult = await publisher.publish(rabbit.createAmqpMessage({ body: `Hello - ${i} - ` }))
35+
switch (publishResult.outcome) {
36+
case rabbit.OutcomeState.ACCEPTED:
37+
console.log("Message Accepted")
38+
break
39+
case rabbit.OutcomeState.RELEASED:
40+
console.log("Message Released")
41+
break
42+
case rabbit.OutcomeState.REJECTED:
43+
console.log("Message Rejected")
44+
break
45+
default:
46+
break
47+
}
48+
}
49+
publisher.close()
50+
51+
console.log("Opening a consumer and consuming messages...")
52+
const consumer = await connection.createConsumer(testQueue, {
53+
messageHandler: (msg) => console.log(`MessageId: ${msg.message_id}; Payload: ${msg.body}`),
54+
})
55+
consumer.start()
56+
await sleep(5000)
57+
58+
console.log("Cleaning up...")
59+
consumer.close()
60+
await management.unbind(routingKey, { source: exchange, destination: queue })
61+
await management.deleteExchange(testExchange)
62+
await management.deleteQueue(testQueue)
63+
management.close()
64+
await connection.close()
65+
await environment.close()
66+
}
67+
68+
main()
69+
.then(() => console.log("done!"))
70+
.catch((res) => {
71+
console.log("ERROR ", res)
72+
process.exit(-1)
73+
})
74+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))

examples/package-lock.json

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"rebuild-source": "cd .. && npm run build && cd - && npm install --force"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"rabbitmq-amqp-js-client": "file:../."
14+
},
15+
"engines": {
16+
"node": "22.x.x"
17+
},
18+
"devDependencies": {
19+
"typescript": "^5.8.3"
20+
}
21+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { Management, AmqpManagement } from "./management.js"
2-
export { Environment, AmqpEnvironment } from "./environment.js"
2+
export { createEnvironment, Environment, AmqpEnvironment } from "./environment.js"
33
export { Connection, AmqpConnection } from "./connection.js"
44
export { Publisher, AmqpPublisher } from "./publisher.js"
55
export { Consumer, AmqpConsumer } from "./consumer.js"
66
export { createAmqpMessage } from "./message.js"
7+
export { OutcomeState } from "./utils.js"

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"outDir": "dist"
1313
},
1414
"include": ["src/**/*"],
15-
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"]
15+
"exclude": ["node_modules", "examples", "**/*.spec.ts", "**/*.test.ts"]
1616
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"outDir": "dist"
1313
},
1414
"include": ["src/**/*", "test/**/*", "vitest.config.mts"],
15-
"exclude": ["node_modules"]
15+
"exclude": ["node_modules", "examples"]
1616
}

vitest.config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default defineConfig({
1818
enabled: false,
1919
provider: "istanbul",
2020
include: ["src/**/*.ts"],
21-
exclude: ["node_modules/**", "dist/**", "test/**"],
21+
exclude: ["node_modules/**", "dist/**", "examples/**", "test/**"],
2222
},
2323
},
2424
})

0 commit comments

Comments
 (0)