A simple broker API
The queuejs is a simple broker API for dummy purposes.
The API was designed to interact with producer and consumer http clients.
To see the endpoints documentation go here
- NestJS for API abstraction;
- Prisma for database abstration;
- async-lock for concurrency management;
- PostgreSQL for database engine on production environment;
- SQLite for database engine on development/test environments;
- Swagger for API documentation;
$ docker-compose up --build
To see API endpoints go to https://localhost:3000/api (Swagger UI)
Before running the code locally, we need to prepare the enviroment.
# install dependencies
$ npm install
# create the database structure (sqlite)
$ npm run pushdb:dev
# generate database abstractions
$ npm run pgenerate:dev
Make sure you have prepared the environment.
# development
$ npm run start
# watch mode (hot reload)
$ npm run start:dev
To see API endpoints go to https://localhost:3000/api (Swagger UI)
Make sure you have prepared the environment.
# create the database structure for integration test (sqlite)
$ npm run pushdb:test
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
Some standalone implementations for producer and consumer are provided.
Currently, the consumer implements at-most-once approach to handle messages.
Example:
- Make sure queuejs is running at
localhost:3000
; - Produce 3 messages (or messages.txt):
echo -e 'message1\nmessage2\nmessage3' | node tools/producer.js -t topic1
message1 OK
message2 OK
message3 OK
- Consume 3 messages:
node tools/consumer.js -t topic1 -w
:
[ { topic: 'topic1', data: 'message1', offset: 1 } ]
[ { topic: 'topic1', data: 'message3', offset: 2 } ]
[ { topic: 'topic1', data: 'message2', offset: 3 } ]
$ node tools/producer.js --help
Usage: producer.js [options]
produce a message for each stdin line to queuejs
Options:
-h, --host <host> The host for queuejs (default: localhost) (default: "localhost")
-p, --port <port> The port for queuejs (default: 3000) (default: "3000")
-t, --topic <topic> topic to produce messages
$ node tools/consumer.js --help
Usage: consumer.js [options]
consume messages from queuejs and write to stdout
Options:
-h, --host <host> The host for queuejs (default: "localhost")
-p, --port <port> The port for queuejs (default: "3000")
-g, --group <group> The consumer group to be used
-t, --topic <topic> topic to produce messages
-w, --watch Keep listening for new messages (default: false)
This API is not supposed to have multiple instances by database. So scaling horizontally is not an option now.
- Improve the API client library and documentation;
- Add support to reset consumer group offset;
- Migrate atomic operations (i.e. produce, commit and consume) to PostgreSQL procedures;
- Add partition key support.