Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bfe6dd0

Browse files
Hello World for nodejs stream client (#403)
* add: simple publish/receive examples for the nodejs stream client
1 parent 3029ca6 commit bfe6dd0

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

‎javascript-nodejs-stream/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

‎javascript-nodejs-stream/README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Node.js code for RabbitMQ stream tutorials
2+
3+
Here you can find JavaScript (Node) code examples from [RabbitMQ tutorials](https://www.rabbitmq.com/getstarted.html) related to the [Stream plugin](https://www.rabbitmq.com/docs/stream).
4+
5+
To successfully use the examples you will need a running RabbitMQ server.
6+
7+
## Requirements
8+
9+
Apart from [Node.js](https://nodejs.org/en/download/), these examples use the [`rabbitmq-stream-js-client`](https://github.com/coders51/rabbitmq-stream-js-client) client library.
10+
11+
## Code
12+
13+
Code examples are executed via `npm`:
14+
15+
npm run publish
16+
npm run receive
17+
18+
To learn more, see [`coders51/rabbitmq-stream-js-client`](https://github.com/coders51/rabbitmq-stream-js-client).

‎javascript-nodejs-stream/package-lock.json‎

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "rabbitmq-stream-node-tutorial",
3+
"version": "1.0.0",
4+
"description": "Tutorial for the nodejs RabbitMQ stream client",
5+
"scripts": {
6+
"send": "node send.js",
7+
"receive": "node receive.js"
8+
},
9+
"dependencies": {
10+
"rabbitmq-stream-js-client": "^0.3.1"
11+
}
12+
}

‎javascript-nodejs-stream/receive.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const rabbit = require("rabbitmq-stream-js-client")
2+
3+
async function main() {
4+
const streamName = "hello-nodejs-stream"
5+
6+
console.log("Connecting...");
7+
const client = await rabbit.connect({
8+
hostname: "localhost",
9+
port: 5552,
10+
username: "guest",
11+
password: "guest",
12+
vhost: "/",
13+
})
14+
15+
console.log("Making sure the stream exists...");
16+
const streamSizeRetention = 5 * 1e9
17+
await client.createStream({ stream: streamName, arguments: { "max-length-bytes": streamSizeRetention } });
18+
19+
console.log("Declaring the consumer with offset...");
20+
await client.declareConsumer({ stream: streamName, offset: rabbit.Offset.first() }, (message) => {
21+
console.log(`Received message ${message.content.toString()}`)
22+
})
23+
24+
}
25+
26+
main()
27+
.then(async () => {
28+
await new Promise(function () { })
29+
})
30+
.catch((res) => {
31+
console.log("Error while receiving message!", res)
32+
process.exit(-1)
33+
})

‎javascript-nodejs-stream/send.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const rabbit = require("rabbitmq-stream-js-client");
2+
3+
async function main() {
4+
const streamName = "hello-nodejs-stream";
5+
6+
console.log("Connecting...");
7+
const client = await rabbit.connect({
8+
vhost: "/",
9+
port: 5552,
10+
hostname: "localhost",
11+
username: "guest",
12+
password: "guest",
13+
});
14+
15+
console.log("Making sure the stream exists...");
16+
const streamSizeRetention = 5 * 1e9
17+
await client.createStream({ stream: streamName, arguments: { "max-length-bytes": streamSizeRetention } });
18+
19+
console.log("Creating the publisher...");
20+
const publisher = await client.declarePublisher({ stream: streamName });
21+
22+
console.log("Sending a message...");
23+
await publisher.send(Buffer.from("Test message"));
24+
25+
console.log("Closing the connection...");
26+
await client.close();
27+
}
28+
29+
main()
30+
.then(() => console.log("done!"))
31+
.catch((res) => {
32+
console.log("Error in publishing message!", res);
33+
process.exit(-1);
34+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /