This small demo project simulates the core of a typical Publisher-Subscriber pattern in real-time and async processing. The publisher sends messages to subscribers registerd to the its instance. As soon as the publisher receives any new requests or input, it promptly forwards them to the subscribers registered for that message signature -topics in this case.
The Subscriptions and information are stored in a small sqlite database included in this repository. The entire architecture is HTTP/REST dependent. And asides the storage of the subscriptions the rest of the process is stateless i.e. once a message is published successfully, it is no longer stored in the system.
Two key classes are the SubscriptionController and PublisherController that handle requests to their endpoints.
The SubscriptionController accepts new requests from external services (simulated via API calls) to subscribe/listen to specific topics. It promptly examines the subscription requests for any anomalies before registering them in the sqlite database. These anomalies are captured in the Feature tests included in the project.
The PublisherController handles all requests to publish a message to listening subscribers. Like the SubscriptionController it inspects the requests for any unexpected anomalies and if it finds none, it promptly sends them to the subscribers.
The tests included in this project are Feature/Integration tests - hence they test a group of classes and methods in one run cycle to validate the functionality of a feature. Both the Publishing and Subcription processes are tested and validated for correctness. Some cases tested for include:
- subscribe to topic
- retrieve all subscriptions
- retrieve all topics to subscription url
- empty subscription url
- non existing topics
- empty broadcast message body
- successful broadcast to subscribers
This project is written entirely in Laravel. To run it on your local machine, pull this project into your desired folder and open the project in any suitable IDE. Open the terminal/commandline in the root of the project and run the following:
composer install
This should run and pull all the dependencies of the project into their necessary folders. Next run the following:
php artisan serve
This will start up the project on port 8000 on your local machine. Once started you can find out if the project is running properly by visiting the link below:
http://localhost:8000/
An empty page showing the the status should appear reading thus: "Up and Running Correctly"
Now the application is ready to recieve requests. ##Endpoints and thier corresponding Payload:
Note: {topic} is a placeholder for a random string that should indicate a topic title.
POST REQUEST -> localhost:8000/subscribe/{topic} -- [topic can be replaced with any random string indicating the topic title]
body -> {
"url": "test.com"
}
GET REQUEST -> localhost:8000/subscribe/{topic} -- [returns all the subscribers to a specific topic if the topic exists]
GET REQUEST -> localhost:8000/subscribe/{topic} -- [returns all the subscribers per topic]
POST REQUEST -> localhost:8000/publish/{topic} -- [publish a message to a topic and forwards the message to all subscribers to that topic]
body -> {
"message": "Your base are belong to us"
}
The project is open-source and licensed under the MIT license.