This project is a microservice that utilizes a websocket to facilitate information exchange between a javascript webapp and a NoSQL database. The microservice (written in python) returns JSON data with the overall score for the requested fantasy sports team. No specific request information is required at this point, but can be implemented if the function of the microservice expands to complete additional processes.
- Python
- JavaScript
- HTML
- asyncio
- Websockets
- JSON
The code block below creates a websocket to request/read data sent from the localhost (127.0.0.1) at the specified port (65432). In this example, "button" is an html button element. When the button is selected, a request is made.
const heading = document.getElementById('main-heading');
const button = document.getElementById('microservice-btn');
// Add event listener to the button
button.addEventListener('click', () => {
const ws = new WebSocket("ws://127.0.0.1:65432/")
// Check if the microservice is running
ws.addEventListener("error", (event) => {
heading.textContent = "Microservice not running.";
});
// No errors, retreive data and update web page
ws.onmessage = function(event){
heading.textContent = event.data;
ws.close();
}
});In the above example, "heading" is an html header element. The "onmessage" function updates "heading" to display the JSON data received from the microservice.
The nested event listener will display error information if the microservice is not running.
// Check if the microservice is running
ws.addEventListener("error", (event) => {
heading.textContent = "Microservice not running.";
});The received JSON data can be reformatted and displayed however necessary.



