0

I need help with communication between a JHipster scaffolded Angular frontend plus Springboot backend. I would like to use the backend's API in sort of doing a request to a Kafka Webflux method, which will continuously fire events to the consumer of the API.

Therefore I wrote this piece of code into a new mqconnect.service.ts class.

    Injectable({
       providedIn: 'root',
    })

    export class MqconnectService {
      url: string = KAFKA_CONSUME_MSG; // contains part of URL : /api/someurl/

      constructor() { this; }

      getMessagesStream(): Observable<StandardMessage> {
        return new Observable<StandardMessage>(observer => {
        const url = this.url;
        const eventSource = new EventSource(url, {withCredentials: true});
        eventSource.onmessage = event => {
          // eslint-disable-next-line no-console
          console.debug('received event: ', event);
          const json = JSON.parse(event.data);
          observer.next(
            new StandardMessage(
              json['Id'],
              // OMITTED
            )
          );
        };
        eventSource.onerror = error => {
          if (eventSource.readyState === 0) {
            // eslint-disable-next-line no-console
            console.log('The stream has been closed by the server.');
            eventSource.close();
            observer.complete();
          } else {
            observer.error('EventSource error: ' + `${String(error)}`);
          }
        };
      });
     }
    }

When I read the logs from Firefox's Developer Console, I will receive always a 401. The Bearer token is not being sent, when doing the request. I wondered, if it has something to do with some interceptor stuff, so I tried to 'get' the token from the Angular Framework.

EDIT: removed useless interceptor.

This will not solve my problem. What's the right way, because IMHO it's a DI Framework and it makes absolutely no sense to create a dangling http.client to make a GET request to the Spring backend.

Semo
  • 783
  • 2
  • 17
  • 38
  • Which auth type? – Gaël Marziou Oct 25 '21 at 10:35
  • Option "JWT" when using the generator. – Semo Oct 25 '21 at 11:11
  • OK, so you have already an interceptor (see auth.interceptor.ts) which shows you that the name of the token in LocalStorage is not the same as the one you put here. Also I'm not sure why you would need two auth interceptors. https://github.com/jhipster/jhipster-sample-app/blob/main/src/main/webapp/app/core/interceptor/auth.interceptor.ts#L23 – Gaël Marziou Oct 25 '21 at 11:58
  • Thank you. Forget the additional interceptor. You're correct, not needed. BUT, why isn't the Token sent with the request of MqconnectService? The header won't get changed (include a token) by the already existing interceptors. I included the ```{withCredentials: true}``` attribute but to no avail. – Semo Oct 25 '21 at 12:08
  • Well I suppose that StandardMessage is provided by your Kafka consumer (what do you use?) but I have no idea how it could run in a browser and it's probably completely unrelated with Angular Http Interceptors. I thought the usual way was rather to use websockets. – Gaël Marziou Oct 25 '21 at 12:56
  • With Postman (API free-to-use test software) I manually made a GET request against the exact same URL. And I used the token, which I consol'ed to the Developer Tools of the browser. Postman returns a 200, this means the spring backend would work fine. StandardMessage is a data exchange model, used across the infrastruct in several parts of my servers. – Semo Oct 25 '21 at 13:56
  • I suppose you have read about EventSource and http headers and found it is not related with Angular http interceptors (see https://stackoverflow.com/questions/28176933/http-authorization-header-in-eventsource-server-sent-events) – Gaël Marziou Oct 25 '21 at 14:09
  • Yes, thank you. In fact I came across a lot of pages here and there for about a week. But I wonder, if EventSource or SSE isn't that supported, why do so many people still try it (based on so many questions regarding EventSource and Angular)? Philosophical question. Also it's strange with the scaffolded jhipster Kafka HTTP interface: it only accepts http requests, instead of a Websocket request. That is how I began and tried to follow the standards given there and wanted to use SSE with http. Wasted time. I think I will reimplement the HTTP interface to a Websocket one in the backend. No idea – Semo Oct 26 '21 at 06:31
  • I suppose the JHipster Kafka implementation is primarily focusing on inter service communication and streaming entities was not fully considered. Once you're done with your implementation, you could submit a feature request on github. – Gaël Marziou Oct 26 '21 at 08:12

0 Answers0