rabbitmq-tutorials/javascript-nodejs-stream/receive.js
icappello bfe6dd09fd
Hello World for nodejs stream client (#403)
* add: simple publish/receive examples for the nodejs stream client
2024-05-09 07:23:05 +02:00

34 lines
973 B
JavaScript

const rabbit = require("rabbitmq-stream-js-client")
async function main() {
const streamName = "hello-nodejs-stream"
console.log("Connecting...");
const client = await rabbit.connect({
hostname: "localhost",
port: 5552,
username: "guest",
password: "guest",
vhost: "/",
})
console.log("Making sure the stream exists...");
const streamSizeRetention = 5 * 1e9
await client.createStream({ stream: streamName, arguments: { "max-length-bytes": streamSizeRetention } });
console.log("Declaring the consumer with offset...");
await client.declareConsumer({ stream: streamName, offset: rabbit.Offset.first() }, (message) => {
console.log(`Received message ${message.content.toString()}`)
})
}
main()
.then(async () => {
await new Promise(function () { })
})
.catch((res) => {
console.log("Error while receiving message!", res)
process.exit(-1)
})