Skip to content

Commit e6d4d4a

Browse files
l4mbymagne
andauthored
[IS-56/feat]: add build for browser (#74)
* feat: add build for browser * chore: fix check * fix: build for node esm and cjs and example * fix: add 500ms after management links creation * chore: update cspell --------- Co-authored-by: magne <[email protected]>
1 parent cf1b72e commit e6d4d4a

21 files changed

+3919
-1538
lines changed

.tool-versions

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
nodejs 22.16.0
1+
nodejs 22.16.0
2+
python 3.13.5

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"ackmode",
55
"ampq",
66
"amqpvalue",
7+
"AMQPWSB",
78
"cacertfile",
89
"certfile",
910
"dste",

examples/package-lock.json

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

examples/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"start": "node index.js",
89
"rebuild-source": "cd .. && npm run build && cd - && npm install --force",
@@ -11,7 +12,8 @@
1112
"author": "",
1213
"license": "ISC",
1314
"dependencies": {
14-
"rabbitmq-amqp-js-client": "file:../."
15+
"rabbitmq-amqp-js-client": "file:../.",
16+
"rhea": "^3.0.4"
1517
},
1618
"engines": {
1719
"node": "22.x.x"

examples/websocket_example.js renamed to examples/socket_example.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
const rabbit = require("rabbitmq-amqp-js-client")
2-
const { randomUUID } = require("crypto")
1+
import { createEnvironment, createAmqpMessage, OutcomeState } from "rabbitmq-amqp-js-client"
2+
import { randomUUID } from "crypto"
33

4-
const rabbitUser = process.env.RABBITMQ_USER ?? "guest"
5-
const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "guest"
4+
const rabbitUser = process.env.RABBITMQ_USER ?? "rabbit"
5+
const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "rabbit"
66
const rabbitHost = process.env.RABBITMQ_HOSTNAME ?? "localhost"
7-
const rabbitPort = process.env.RABBITMQ_PORT ?? 15678
7+
const rabbitPort = process.env.RABBITMQ_PORT ?? 5672
88

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

1414
console.log("Creating the environment...")
15-
const environment = rabbit.createEnvironment({
15+
const environment = createEnvironment({
1616
host: rabbitHost,
1717
port: rabbitPort,
1818
username: rabbitUser,
1919
password: rabbitPassword,
20-
webSocket: WebSocket,
2120
})
2221

2322
console.log("Opening a connection...")
@@ -34,15 +33,15 @@ async function main() {
3433
console.log("Opening a publisher and publishing 10 messages...")
3534
const publisher = await connection.createPublisher({ exchange: { name: testExchange, routingKey: routingKey } })
3635
for (const i of Array(10).keys()) {
37-
const publishResult = await publisher.publish(rabbit.createAmqpMessage({ body: `Hello - ${i} - ` }))
36+
const publishResult = await publisher.publish(createAmqpMessage({ body: `Hello - ${i} - ` }))
3837
switch (publishResult.outcome) {
39-
case rabbit.OutcomeState.ACCEPTED:
38+
case OutcomeState.ACCEPTED:
4039
console.log("Message Accepted")
4140
break
42-
case rabbit.OutcomeState.RELEASED:
41+
case OutcomeState.RELEASED:
4342
console.log("Message Released")
4443
break
45-
case rabbit.OutcomeState.REJECTED:
44+
case OutcomeState.REJECTED:
4645
console.log("Message Rejected")
4746
break
4847
default:

examples/websocket_example.html

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>AMQP websockets example</title>
5+
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
6+
<script src="../dist/index.umd.js"></script>
7+
</head>
8+
9+
<body>
10+
<input type="text" id="request" style="width: 100%" />
11+
<script type="module">
12+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
13+
const testExchange = `test-exchange-browser-example`
14+
const testQueue = `test-queue-browser-example`
15+
const routingKey = `test-key-browser-example`
16+
console.log("Creating the environment through a websocket...")
17+
const environment = RabbitmqAmqpClient.createEnvironment({
18+
host: "localhost",
19+
port: 15678,
20+
username: "rabbit",
21+
password: "rabbit",
22+
webSocket: { implementation: WebSocket },
23+
})
24+
25+
console.log("Opening a connection...")
26+
const connection = await environment.createConnection()
27+
const management = connection.management()
28+
29+
console.log("Creating a queue and an exchange...")
30+
const queue = await management.declareQueue(testQueue)
31+
const exchange = await management.declareExchange(testExchange)
32+
33+
console.log("Binding exchange to queue...")
34+
await management.bind(routingKey, { source: exchange, destination: queue })
35+
36+
console.log("Opening a publisher and publishing 10 messages...")
37+
const publisher = await connection.createPublisher({ exchange: { name: testExchange, routingKey: routingKey } })
38+
for (const i of Array(10).keys()) {
39+
const publishResult = await publisher.publish(RabbitmqAmqpClient.createAmqpMessage({ body: `Hello - ${i} - ` }))
40+
switch (publishResult.outcome) {
41+
case RabbitmqAmqpClient.OutcomeState.ACCEPTED:
42+
console.log("Message Accepted")
43+
break
44+
case RabbitmqAmqpClient.OutcomeState.RELEASED:
45+
console.log("Message Released")
46+
break
47+
case RabbitmqAmqpClient.OutcomeState.REJECTED:
48+
console.log("Message Rejected")
49+
break
50+
default:
51+
break
52+
}
53+
}
54+
publisher.close()
55+
56+
console.log("Opening a consumer and consuming messages...")
57+
const consumer = await connection.createConsumer({
58+
queue: { name: testQueue },
59+
messageHandler: (context, msg) => {
60+
context.accept()
61+
console.log(`MessageId: ${msg.message_id}; Payload: ${msg.body}`)
62+
},
63+
})
64+
consumer.start()
65+
await sleep(5000)
66+
67+
console.log("Cleaning up...")
68+
consumer.close()
69+
await management.unbind(routingKey, { source: exchange, destination: queue })
70+
await management.deleteExchange(testExchange)
71+
await management.deleteQueue(testQueue)
72+
management.close()
73+
await connection.close()
74+
await environment.close()
75+
</script>
76+
</body>
77+
</html>

0 commit comments

Comments
 (0)