A PubSub API using expressjs + typescript + docker.
- Create subscription to topics.
- Publish messages to topic subscribers.
- Express.js as the web framework.
- Linting with ESlint.
- Testing with Mocha.
- Testing with Chai.
- Containerize with Docker.
# Clone the project
git clone https://github.com/sauban/pubsub-server.git
cd pubsub-server
# Install dependencies
yarn install
# Copy .env.example file
cp .env.example .env
Set Environment Variables
MONGODB_URL=<mongodburl>
PORT=9094
Then you can start the application:
npm start
This will launch the server node process on port 9094
To create subscription for topics
- API Endpoint
<BASE_URL>/subscribe/:topic
- Request Payload
POST /subscribe/topic1
{
"url": "http://localhost:3000/test1"
}
This should return a response as shown below:
{
"url": "http://localhost:3000/test1"
}
To publish messages under topic to subscribers
- API Endpoint
<BASE_URL>/publish/:topic
- Request Payload
POST /publish/topic1
{
"message": "Hello world"
}
This should return the payload as shown below:
{
"topic": "topic1",
"data": {
"message": "Hello world"
}
}
Run below to start the pubsub server and the subscriber server
chmod +x ./start-server.sh
./start-server.sh
Open a new terminal and run the following:
curl -X POST -H "Content-Type: application/json" -d '{ "url": "http://localhost:4043/hello"}' http://localhost:9009/subscribe/topic1
curl -X POST -H "Content-Type: application/json" -d '{ "url": "http://localhost:4043/hello2"}' http://localhost:9009/subscribe/topic1
curl -X POST -H "Content-Type: application/json" -d '{"message": "hello"}' http://localhost:9009/publish/topic1