You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Languages Used
Python
JavaScript
HTML
Python Packages Used
asyncio
Websockets
JSON
Requesting/Receiving Data
Requesting
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.
constheading=document.getElementById('main-heading');constbutton=document.getElementById('microservice-btn');// Add event listener to the buttonbutton.addEventListener('click',()=>{constws=newWebSocket("ws://127.0.0.1:65432/")// Check if the microservice is runningws.addEventListener("error",(event)=>{heading.textContent="Microservice not running.";});// No errors, retreive data and update web pagews.onmessage=function(event){heading.textContent=event.data;ws.close();}});
Receiving
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 runningws.addEventListener("error",(event)=>{heading.textContent="Microservice not running.";});
The received JSON data can be reformatted and displayed however necessary.
Screenshots
Before data request
After data request
Microservice not running when request is made
UML Diagram
About
Websocket microservice to facilitate communication between a javascript webapp and NoSQL database.