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
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nodejs 22.16.0
nodejs 22.16.0
python 3.13.5
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ackmode",
"ampq",
"amqpvalue",
"AMQPWSB",
"cacertfile",
"certfile",
"dste",
Expand Down
48 changes: 44 additions & 4 deletions examples/package-lock.json

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

4 changes: 3 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js",
"rebuild-source": "cd .. && npm run build && cd - && npm install --force",
Expand All @@ -11,7 +12,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"rabbitmq-amqp-js-client": "file:../."
"rabbitmq-amqp-js-client": "file:../.",
"rhea": "^3.0.4"
},
"engines": {
"node": "22.x.x"
Expand Down
21 changes: 10 additions & 11 deletions examples/websocket_example.js → examples/socket_example.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
const rabbit = require("rabbitmq-amqp-js-client")
const { randomUUID } = require("crypto")
import { createEnvironment, createAmqpMessage, OutcomeState } from "rabbitmq-amqp-js-client"
import { randomUUID } from "crypto"

const rabbitUser = process.env.RABBITMQ_USER ?? "guest"
const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "guest"
const rabbitUser = process.env.RABBITMQ_USER ?? "rabbit"
const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "rabbit"
const rabbitHost = process.env.RABBITMQ_HOSTNAME ?? "localhost"
const rabbitPort = process.env.RABBITMQ_PORT ?? 15678
const rabbitPort = process.env.RABBITMQ_PORT ?? 5672

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({
const environment = createEnvironment({
host: rabbitHost,
port: rabbitPort,
username: rabbitUser,
password: rabbitPassword,
webSocket: WebSocket,
})

console.log("Opening a connection...")
Expand All @@ -34,15 +33,15 @@ async function main() {
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} - ` }))
const publishResult = await publisher.publish(createAmqpMessage({ body: `Hello - ${i} - ` }))
switch (publishResult.outcome) {
case rabbit.OutcomeState.ACCEPTED:
case OutcomeState.ACCEPTED:
console.log("Message Accepted")
break
case rabbit.OutcomeState.RELEASED:
case OutcomeState.RELEASED:
console.log("Message Released")
break
case rabbit.OutcomeState.REJECTED:
case OutcomeState.REJECTED:
console.log("Message Rejected")
break
default:
Expand Down
77 changes: 77 additions & 0 deletions examples/websocket_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!doctype html>
<html>
<head>
<title>AMQP websockets example</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script src="../dist/index.umd.js"></script>
</head>

<body>
<input type="text" id="request" style="width: 100%" />
<script type="module">
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const testExchange = `test-exchange-browser-example`
const testQueue = `test-queue-browser-example`
const routingKey = `test-key-browser-example`
console.log("Creating the environment through a websocket...")
const environment = RabbitmqAmqpClient.createEnvironment({
host: "localhost",
port: 15678,
username: "rabbit",
password: "rabbit",
webSocket: { implementation: WebSocket },
})

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(RabbitmqAmqpClient.createAmqpMessage({ body: `Hello - ${i} - ` }))
switch (publishResult.outcome) {
case RabbitmqAmqpClient.OutcomeState.ACCEPTED:
console.log("Message Accepted")
break
case RabbitmqAmqpClient.OutcomeState.RELEASED:
console.log("Message Released")
break
case RabbitmqAmqpClient.OutcomeState.REJECTED:
console.log("Message Rejected")
break
default:
break
}
}
publisher.close()

console.log("Opening a consumer and consuming messages...")
const consumer = await connection.createConsumer({
queue: { name: testQueue },
messageHandler: (context, msg) => {
context.accept()
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()
</script>
</body>
</html>
Loading