I have a controller in Nestjs that handles an HTTP request. There is an IoT device that communicates with the server and sends the latest changes to the server with a post request. At the same time, there is a mobile application that should receive the update in realtime with Websockets.
I know that the HTTP request and Websockets are different concepts in Nestjs, but is there any way to emit an event whenever I receive the HTTP request?
Any idea would be highly appreciated.
There is indeed a proper solution for this. You need an Injectable that contains an RxJS Subject. Whenever your Controller receives a value via the POST request, it delegates the value to the injected service. The service then "instructs" the Subject to emit the value.
On the other side of the chain, inside your WebSocket Gateway #SubscribeMessage, you return an RxJS Observable - which is derived from the Subject - to the connected clients.
More on Subject and Asynchronous responses
Here is the implementation.
Related
I have a structure where an application sends a POST request, the API handles the parameters and enters them into the database then another application that is supposed to run a function when the POST request was handled. The only issue I'm having is how can I make the 2nd application know when the request was handled other than running a GET request on a time interval?
Another explanation:
Client A: POSTs data
API: Handles data
Client B: GETs and displays data to user
How can I tell Client B when to do the GET request?
This is all in node.js and using express
What you need is to push data to clients when something happens server-side. This can be achieved by either server side events (which do exactly this), or websockets which create a bidirectional communication channel between server and client. Which one to choose? Check out this stack overflow post.
I use NestJS app with http-proxy-middleware to proxy incoming request to the backend server.
If incoming request matches the proxy config it will handle the request and send response directly to the client without invoking standard route handling mechanism of the NestJS app.
Is there any way to process this same request with standard route handling mechanism in NestJS?
As an example what I want to achieve is to send Websocket message, SSE event, or run background task using NestJS capabilities based on response received from backend service.
Solutions I think of so far are the following:
Create a custom proxy middleware where instead of response.send() it will be calling next(), but may be some other proxy-middleware solution out there that would allow this?
Create interceptor, it will allow me to make a backend call and modify response based on backed response and then call next(), not sure if this will work.
I've got an chatbot app where I want to send one message e.g. res.json("Hello") from express, then another message later e.g. res.json("How are you doing"), but want to process some code between the two.
My code seems to have some problems with this, because when I delete the first res.json() then the second one works fine and doesn't cause any problems.
Looking in my heroku logs, I get lots of gobbledy gook response from the server, with an IncomingMessage = {}, containing readableState and Server objects when I include both of these res.json() functions.
Any help would be much appreciated.
HTTP is request/response. Client sends a request, server sends ONE response. Your first res.json() is your ONE response. You can't send another response to that same request. If it's just a matter of collecting all the data before sending the one response, you can rethink your code to collect all the data before sending the one response.
But, what you appear to be looking for is "server push" where the server can send data to the client continually whenever it wants to. The usual solution for that is a webSocket connection (or socket.io which is built on top of webSocket and adds more features).
In the webSocket/socket.io architecture, the client makes a connection the server and the connection is kept open indefinitely. Then either side of the connection can send messages to the other end. This is most useful when the server wants to "push" data to the client at any time. In this case, the client establishes the connection, then the server can send data to the client over that connection at any time. The client registers a listener for incoming messages and will be notified anytime the server sends it some data.
Both webSocket and socket.io are fully supported in modern browsers and in node.js. I would personally recommend using socket.io because some of the features it adds (a messaging layer, auto-reconnect, etc...) are very useful.
To use a continuously connected socket like this, you will have to make sure your hosting infrastructure is properly configured to allow it.
res.json() always sends the response to the client immediately (calling it again will cause an error). If you need to gradually build up a response then you can progressively decorate a plain old javascript object; for example, appending items to an array. When you are done call res.json() with the constructed response.
But you should post your code so we can see what's happening.
I have one question.
In my current application I am using express, and I and using Express.get and Express.post method multiple times because I have multiple pages.
So, I want to intercept each response before sending it to client to add some state in response object.
Is there any way in express.js by which I can intercept each response before sending it to client.
In my node.js server app I'm providing a service to my js client that performs some handling of remote api's.
It might very well be possible that two different clients request the same information. Say client 1 requests information, then before client 1's request is fully handled (remote api's didn't returns their response yet) client 2 is requesting the same data. What I'd want to is to wait for client 1 data to be ready and then write it to both client 1 and 2.
This seems to me like a very common issue and I was wondering if there was any library or built-in support in connect or express that supports this issue.
You might not want to use HTTP for providing the data to the client. Reasons:
If the remote API is taking a lot of time to process you will risk the client request to timeout, or the browser to repeat the request.
You will have to share some state between requests which is not a good practice.
Have a look at websockets (socket.io would be a place to start). With them you can push data from the server to the client. In your scenario, clients will perform the request to the server, which will return 202 and when the remote API will respond, the server will push the data to the clients using websockets.