Skip to content

sauban/pubsub-server

Repository files navigation

PubSub Server

A PubSub API using expressjs + typescript + docker.

Features

  • Create subscription to topics.
  • Publish messages to topic subscribers.

Technologies

Getting started

# 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

Subscribing

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"
}

Publishing

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"
  }
}

Use Shell script

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published