Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 2 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# RabbitMQ AMQP 1.0 JavaScript Client

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

[![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)

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

## Getting started

**NOTE:** This is just a first example and will be replaced with a reference to the _examples folder_

The following example demonstrates how to create an environment, open a connection, and use the management to create and delete queues, exchanges, and bindings.

```typescript
const environment = createEnvironment({
host: "localhost",
port: 5672,
username: "rabbit",
password: "rabbit",
})

const connection = await environment.createConnection()

const management = connection.management()

const queue = await management.declareQueue("test")

const exchange = await management.declareExchange("exchange", { type: "topic" })
const secondExchange = await management.declareExchange("exchange-dest", { type: "topic" })

const bindingToQueue = await management.bind("foo", { source: exchange, destination: queue })
const bindingToExchange = await management.bind("foo", { source: exchange, destination: secondExchange })

await management.unbind("foo", { source: exchange, destination: queue })
await management.unbind("foo", { source: exchange, destination: secondExchange })

await management.deleteExchange("exchange")
await management.deleteExchange("exchange-dest")
await management.deleteQueue("test")

management.close()
await connection.close()
await environment.close()
```

Inside the [_examples_](./examples/) folder you can find a node project that shows how to use the library.

## Resources

Expand Down
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Client examples

- [Basic Usage](./index.js) - Producer and Consumer example without reconnection
74 changes: 74 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const rabbit = require("rabbitmq-amqp-js-client")
const { randomUUID } = require("crypto")

const rabbitUser = process.env.RABBITMQ_USER || "rabbit"
const rabbitPassword = process.env.RABBITMQ_PASSWORD || "rabbit"

async function main() {
const testExchange = `test-exchange-${randomUUID()}`
const testQueue = `test-queue-${randomUUID()}`
const routingKey = `test-key-${randomUUID()}`

console.log("Creating the environment...")
const environment = rabbit.createEnvironment({
host: "localhost",
port: 5672,
username: rabbitUser,
password: rabbitPassword,
})

console.log("Opening a connection...")
const connection = await environment.createConnection()
const management = connection.management()

console.log("Creating a queue and an exchange...")
const queue = await management.declareQueue(testQueue)
const exchange = await management.declareExchange(testExchange)

console.log("Binding exchange to queue...")
await management.bind(routingKey, { source: exchange, destination: queue })

console.log("Opening a publisher and publishing 10 messages...")
const publisher = await connection.createPublisher({ exchange: { name: testExchange, routingKey: routingKey } })
for (const i of Array(10).keys()) {
const publishResult = await publisher.publish(rabbit.createAmqpMessage({ body: `Hello - ${i} - ` }))
switch (publishResult.outcome) {
case rabbit.OutcomeState.ACCEPTED:
console.log("Message Accepted")
break
case rabbit.OutcomeState.RELEASED:
console.log("Message Released")
break
case rabbit.OutcomeState.REJECTED:
console.log("Message Rejected")
break
default:
break
}
}
publisher.close()

console.log("Opening a consumer and consuming messages...")
const consumer = await connection.createConsumer(testQueue, {
messageHandler: (msg) => console.log(`MessageId: ${msg.message_id}; Payload: ${msg.body}`),
})
consumer.start()
await sleep(5000)

console.log("Cleaning up...")
consumer.close()
await management.unbind(routingKey, { source: exchange, destination: queue })
await management.deleteExchange(testExchange)
await management.deleteQueue(testQueue)
management.close()
await connection.close()
await environment.close()
}

main()
.then(() => console.log("done!"))
.catch((res) => {
console.log("ERROR ", res)
process.exit(-1)
})
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
61 changes: 61 additions & 0 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"rebuild-source": "cd .. && npm run build && cd - && npm install --force"
},
"author": "",
"license": "ISC",
"dependencies": {
"rabbitmq-amqp-js-client": "file:../."
},
"engines": {
"node": "22.x.x"
},
"devDependencies": {
"typescript": "^5.8.3"
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { Management, AmqpManagement } from "./management.js"
export { Environment, AmqpEnvironment } from "./environment.js"
export { createEnvironment, Environment, AmqpEnvironment } from "./environment.js"
export { Connection, AmqpConnection } from "./connection.js"
export { Publisher, AmqpPublisher } from "./publisher.js"
export { Consumer, AmqpConsumer } from "./consumer.js"
export { createAmqpMessage } from "./message.js"
export { OutcomeState } from "./utils.js"
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"]
"exclude": ["node_modules", "examples", "**/*.spec.ts", "**/*.test.ts"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"outDir": "dist"
},
"include": ["src/**/*", "test/**/*", "vitest.config.mts"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "examples"]
}
2 changes: 1 addition & 1 deletion vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
enabled: false,
provider: "istanbul",
include: ["src/**/*.ts"],
exclude: ["node_modules/**", "dist/**", "test/**"],
exclude: ["node_modules/**", "dist/**", "examples/**", "test/**"],
},
},
})