Sunday, 27 May 2018

WebSockets and HTTP/RESTful API on one server?

Hey,I want the following/have the following scenario: I'm about to build an application (MERN stack) consiting of a RESTful API which is provided by node.js, a database (mongo) which stores data POSTed to the RESTful API and a react application, which can retrieve data from the database via the REST API.The data I want to store are most likely system information/technical details like storage (empty space etc.), services (stopped/running), files in form of binary data etc. The data gets provided by different remote clients (keep in mind, there will be multiple remote clients, but only one web client). Remote clients in this case are basically other systems.I'm looking for a way to store the data on the server. I thought about the following concept: my node.js server would not only provide the REST API but also allow websockets to connect to it. Why Websockets? Because I need the server to be able to request from the client at any time(which is what websockets are for, http only provides communication from the client to the server). For the remote clients I wrote an application in JAVA, which is able to connect to my node.js server via a socket and response to requests from the server. This works great so far.To sum it up: currently a user can login to the web client (react app) and access the REST api provided by my node.js server to GET data from the database. Works perfectly.At this moment however only the server can request data from any remote client via a socket. The ultimate goal would be to somehow give the user using the web client an option to execute/trigger this request, so the user can request up to date information from any remote client (the data get's stored by the node.js server, which uses the provided REST api itself by using the request lib).Where would I implement this function/method though? How about creating another route for my REST API which exeactly does this, e.g.(just basic examples to keep it simple):https://localhost:3000/clients/client0/request?type=servicehttps://localhost:3000/clients/client0/request?type=filehttps://localhost:3000/clients/client0/request?type=storageBehind this kind of route the server would execute a request towards the specified remote client(in this case client0) via a socket. The remote client would respond to the request. The server would process the response by calling the provided REST api routes to POST data and store it tot he database. The up to date data could now be retrieved via GET from the web client. The entire process would enable the user to request up to date data from any remote client. Which http method should I choose for this kind of route though?Is there anything wrong with this approach? Any bad ideas? Any other options? I would be happy to hear your thoughts about this and I hope I explained my issue sufficiently.

Submitted May 27, 2018 at 10:53AM by Fasyx

No comments:

Post a Comment