-1

The following basic code is used to handle requests from Angular client:

/************************************************************************/
/*  Launch HTTP server            
/************************************************************************/
http.createServer (function(req,res) {
    
    let data='';
    
    req.on ('data', chunk => {
        //console.log (`Data chunk: ${chunk}`);
        
        //append chunk to data (can be multiple chuncks for 1 request)
        data += chunk;
    });
    
    req.on ('end', chunks => {
        //console.log (`End chunks: ${data}`);
            
    //Do something with request 
    });
}).listen (8000);

The HTTP request is converted to TCP raw message and sent to 3rd party server. This external server sends back TCP response which is sent to the Angular client.

The response sent back from Node.js to the client is not according to the order of original requests. So an HTTP request in the client is getting a wrong response. The client has multiple timers each sending a request every 1 second.

I want that while a client request is handled, Node.js will not accept any other new messages.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Zvi Vered
  • 459
  • 2
  • 8
  • 16
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – derpirscher Oct 28 '22 at 06:54

2 Answers2

0

Server doesn't care about the order of requests, it receives the request, does its job and sends back a response. The order you need to organize should be in client side

  • Hi Alexey. Thank you for your comment. Can you please tell how the Angular client can organize the order of responses ? In case requests are send asynchronously from multiple places. – Zvi Vered Oct 28 '22 at 09:41
0

The solution I found is to implement an Http interceptor in order to monitor the response from the server. In case the response contains a reply that has to be displayed, from the interceptor I called an Update method implemented in the relevant component.

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpEvent, HttpResponse, HttpRequest, HttpHandler } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map} from 'rxjs/operators';
import { TelemetryComponent } from '../components/telemetry/telemetry.component';
import { InterruptsComponent } from '../components/interrupts/interrupts.component';
import {SharedobjectService} from 'src/app/service/sharedobject.service';
import { E_PC_TO_TGT_CODE} from 'src/app/icd/Header'

@Injectable()
export class Interceptor implements HttpInterceptor {

  telemetry : TelemetryComponent;
  interrupts : InterruptsComponent;

  /******************************************************************************************/
  constructor(private shared : SharedobjectService){
  }

  /******************************************************************************************/
  intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(httpRequest).pipe (
      map (resp =>{
        if (resp instanceof HttpResponse)
        {
          
          var arr32 = new Int32Array (resp.body);
          switch (arr32[1])
          {
          case E_PC_TO_TGT_CODE.READ_TELEMETRY_REQUEST_CODE:
            this.shared.telemetry.UpdateView (resp.body); 
            break;

          case E_PC_TO_TGT_CODE.READ_INTERRUPTS_REQUEST_CODE:
            this.shared.interrupts.UpdateView (resp.body);
            break;
          }
        }
        return resp;
       })
    )
  }

}

According to many links I found, Node.js cannot "wait till client request is handled". Is there a better solution ?

Thank you, Zvika

Zvi Vered
  • 459
  • 2
  • 8
  • 16