Skip to content

Commit a9f6827

Browse files
99 setup base docs and example (#118)
* update readme with basic docs publish and consume * update readme * update example with basic publish/consume * add effective package to example * align package.json * chore: minor cleanup * chore: removed not ready for prod in readme * fix: fixed example * fix some typos * change example to use file package --------- Co-authored-by: Luca <[email protected]>
1 parent 81204cb commit a9f6827

File tree

5 files changed

+128
-60
lines changed

5 files changed

+128
-60
lines changed

README.md

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
# RabbitMQ client for the stream protocol for Node.JS
22

3-
# NOT READY FOR PRODUCTION - The client is HEAVILY work in progress.
4-
53
[![Build Status](https://github.com/coders51/rabbitmq-stream-js-client/actions/workflows/main.yml/badge.svg)](https://github.com/coders51/rabbitmq-stream-js-client/actions)
64

7-
# Table of Contents
5+
## Table of Contents
86

9-
- [Overview](#overview)
10-
- [Installing via NPM](#installing-via-npm)
11-
- [Getting started](#getting-started)
7+
- [RabbitMQ client for the stream protocol for Node.JS](#rabbitmq-client-for-the-stream-protocol-for-nodejs)
8+
- [Overview](#overview)
9+
- [Installing via NPM](#installing-via-npm)
10+
- [Getting started](#getting-started)
1211
- [Usage](#usage)
1312
- [Connect](#connect)
14-
- [Build from source](#build-from-source)
15-
- [Project Status](#project-status)
16-
- [Release Process](#release-process)
13+
- [Basic Publish](#basic-publish)
14+
- [Basic Consuming](#basic-consuming)
15+
- [Running Examples](#running-examples)
16+
- [Build from source](#build-from-source)
17+
- [MISC](#misc)
18+
19+
## Overview
20+
21+
A client for the RabbitMQ stream protocol, written (and ready for) Typescript
22+
23+
## Installing via NPM
24+
25+
```shell
26+
npm install rabbitmq-stream-js-client
27+
```
1728

1829
## Getting started
1930

20-
A rapid getting started
31+
A quick getting started
2132

2233
```typescript
2334
const rabbit = require("rabbitmq-stream-js-client")
@@ -59,31 +70,95 @@ const client = await connect({
5970
await client.close()
6071
```
6172

73+
### Basic Publish
74+
75+
```typescript
76+
const client = await connect({
77+
hostname: "localhost",
78+
port: 5552,
79+
username: "rabbit",
80+
password: "rabbit",
81+
vhost: "/",
82+
})
83+
84+
const publisher = await client.declarePublisher({
85+
stream: "stream-name",
86+
publisherRef: "my-publisher",
87+
})
88+
89+
await publisher.send(Buffer.from("my message content"))
90+
91+
// ...
92+
93+
await client.close()
94+
```
95+
96+
### Basic Consuming
97+
98+
```typescript
99+
const client = await connect({
100+
hostname: "localhost",
101+
port: 5552,
102+
username: "rabbit",
103+
password: "rabbit",
104+
vhost: "/",
105+
})
106+
107+
const consumerOptions = { stream: "stream-name", offset: Offset.next() } // see docs for various offset types
108+
109+
const consumer = await client.declareConsumer(consumerOptions, (message: Message) => {
110+
console.log(message.content) // it's a Buffer
111+
})
112+
113+
// ...
114+
115+
await client.close()
116+
```
117+
118+
## Running Examples
119+
120+
the folder /example contains a project that shows some examples on how to use the lib, to run it follow this steps
121+
122+
move to the example folder and install the dependencies
123+
124+
```shell
125+
cd example
126+
npm i
127+
```
128+
129+
run the docker-compose to launch a rabbit instance already stream enabled
130+
131+
```shell
132+
docker-compose up -d
133+
```
134+
135+
then launch the examples
136+
137+
```shell
138+
npm start
139+
```
140+
62141
## Build from source
63142

64143
Build:
65144

66145
```shell
67-
$ npm run build
146+
npm run build
68147
```
69148

70149
Test:
71150

72151
```shell
73-
$ docker-compose up -d
74-
$ npm run test
152+
docker-compose up -d
153+
npm run test
75154
```
76155

77156
Check everything:
78157

79158
```shell
80-
$ npm run check
159+
npm run check
81160
```
82161

83-
## Project Status
84-
85-
The client is HEAVILY work in progress. The API(s) could change prior to version `1.0.0`
86-
87162
## MISC
88163

89-
https://github.com/rabbitmq/rabbitmq-server/blob/master/deps/rabbitmq_stream/docs/PROTOCOL.adoc
164+
<https://github.com/rabbitmq/rabbitmq-server/blob/master/deps/rabbitmq_stream/docs/PROTOCOL.adoc>

example/docker-compose.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "2"
2+
3+
services:
4+
rabbitmq-stream:
5+
image: rabbitmq:3-management
6+
container_name: rabbitmq-stream
7+
restart: unless-stopped
8+
hostname: "rabbitmq"
9+
ports:
10+
- "15672:15672"
11+
- "5672:5672"
12+
- "5552:5552"
13+
environment:
14+
RABBITMQ_DEFAULT_USER: "rabbit"
15+
RABBITMQ_DEFAULT_PASS: "rabbit"
16+
volumes:
17+
- ../conf/enabled_plugins:/etc/rabbitmq/enabled_plugins

example/index.js

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
const rabbit = require("rabbitmq-stream-js-client")
2-
const amqplib = require("amqplib")
32
const { randomUUID } = require("crypto")
43

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

87
async function main() {
98
const streamName = `example-${randomUUID()}`
10-
console.log(`Create stream ${streamName}`)
9+
console.log(`Creating stream ${streamName}`)
1110

1211
const client = await rabbit.connect({
1312
hostname: "localhost",
@@ -20,52 +19,21 @@ async function main() {
2019
await client.createStream({ stream: streamName, arguments: {} })
2120
const producer = await client.declarePublisher({ stream: streamName })
2221

23-
await producer.send(Buffer.from("ciao"))
22+
await producer.send(Buffer.from("Test message"))
2423

25-
await createClassicConsumer(streamName)
24+
await client.declareConsumer({ stream: streamName, offset: rabbit.Offset.first() }, (message) => {
25+
console.log(`Received message ${message.content.toString()}`)
26+
})
2627

27-
await client.deleteStream({ stream: streamName })
28+
await sleep(2000)
2829

2930
await client.close()
3031
}
3132

32-
/**
33-
*
34-
* @param {string} queueName name of the stream to consume
35-
* @returns {Promise<void>}
36-
*/
37-
async function createClassicConsumer(queueName) {
38-
return new Promise(async (res, rej) => {
39-
try {
40-
const conn = await amqplib.connect(`amqp://${rabbitUser}:${rabbitPassword}@localhost`)
41-
const ch = await conn.createChannel()
42-
await ch.prefetch(1000)
43-
await ch.consume(
44-
queueName,
45-
async (msg) => {
46-
if (!msg) {
47-
return
48-
}
49-
50-
console.log("ACK", msg.content.toString())
51-
ch.ack(msg)
52-
if (msg.content.toString() === "ciao") {
53-
await ch.close()
54-
await conn.close()
55-
res()
56-
}
57-
},
58-
{ arguments: { "x-stream-offset": "first" } }
59-
)
60-
} catch (err) {
61-
rej(err)
62-
}
63-
})
64-
}
65-
6633
main()
6734
.then(() => console.log("done!"))
6835
.catch((res) => {
6936
console.log("ERROR ", res)
7037
process.exit(-1)
7138
})
39+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))

example/package-lock.json

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
"devDependencies": {
2020
"typescript": "^4.9.5"
2121
}
22-
}
22+
}

0 commit comments

Comments
 (0)