Implementing SSE in angular

Discussão em 'Angular' iniciado por user21921938, Dezembro 5, 2024.

  1. user21921938

    user21921938 Guest

    I'm trying to implement SSE in my test application. The server side has been set up and I'm just using the endpoint (api/v1/sse/document). The scenario is that when I do a scan, the scan should appear in my test client as well as my main application. The problem is that it only appears on my test client when I refresh the page. I've been writing some code but I'm still able to automatically update the events in my test client.

    this is my code that I wrote

    sse.service.ts

    [document-list.component.ts]

    public ngOnInit(): void {
    this.getDocuments();
    this.registerServerSentEvent();
    }

    public ngOnDestroy(): void {
    this.closeServerSentEvent();
    }

    /**
    * getDocuments: call the api service to get the list of documents.
    */
    public getDocuments(): void {
    this.aService.getDocuments().subscribe((documents: Documents[]) => {
    this.documents = documents;
    });
    }

    /**
    * markDocumentAsProcessed: call the api service to mark the document as processed.
    */
    public markDocumentAsProcessed(document: Documents): void {
    this.aService.markDocumentProcessed(document.document.id).subscribe({
    next: () => {
    // Remove the processed document from the list
    this.documents = this.documents.filter((doc) => doc.document.id !== document.document.id);
    this.closeDialog();
    },
    error: (error) => {
    console.log("markDocumentProcessed Error:", error);
    // Handle the error here
    },
    });
    }

    /**
    * showDetails: call the api service to get the document image and display it in a dialog.
    */
    public showDetails(document: Documents): void {
    this.aService.getDocumentImage(document.document.id).subscribe((image: Blob) => {
    const url = window.URL.createObjectURL(image);
    const safeUrl: SafeUrl = this.sanitizer.bypassSecurityTrustUrl(url);
    this.selectedDocument = {...document, imageDataURL: safeUrl};
    this.displayDialog = true;
    });
    }

    /**
    * closeDialog: close the dialog after pressing the process button.
    */
    private closeDialog(): void {
    this.displayDialog = false;
    this.selectedDocument = null;
    }

    private registerServerSentEvent(): void {
    const sseUrl = `${this.aService.config.url}api/v1/sse/document`;
    this.sseSubscription = this.sseService.getServerSentEvent(sseUrl).subscribe((event: MessageEvent) => {
    const documentEvent = JSON.parse(event.data);
    const eventType = documentEvent.type;
    const eventData = documentEvent.data;

    switch (eventType) {
    case "NewDocument": {
    // Process new document event
    break;
    }

    case "ViewDocument": {
    // Process view document event
    break;
    }

    case "WatchlistStatusUpdate": {
    // Process watchlist status update event
    break;
    }

    case "DocumentProcessed": {
    // Process document processed event
    const processedDocumentId = eventData.documentId;
    this.updateProcessedDocument(processedDocumentId);
    break;
    }
    }
    });
    }

    private updateProcessedDocument(processedDocumentId: string): void {
    // Find the processed document in the documents list
    const processedDocumentIndex = this.documents.findIndex((doc) => doc.document.id === processedDocumentId);
    if (processedDocumentIndex !== -1) {
    // Remove the processed document from the list
    this.documents.splice(processedDocumentIndex, 1);
    // Update any other UI-related logic or perform additional actions as needed
    }
    }

    private closeServerSentEvent(): void {
    if (this.sseSubscription) {
    this.sseSubscription.unsubscribe();
    this.sseService.closeEventSource();
    }
    }
    }


    and this is my service that gets the document and etc

    [a.service.ts]

    public getDocuments(): Observable<Documents[]> {
    let url = this.config.url;
    if (!url.endsWith("/")) {
    url += "/";
    }

    return this.http
    .get<Documents[]>(`${url}api/v1/document/`, {
    headers: {
    authorization: this.config.authorization,
    },
    })
    .pipe(
    map((data) => {
    return data;
    }),
    catchError((error) => {
    throw error;
    })
    );
    }

    /**
    * markDocumentProcessed: calls the api service to mark the document as processed.
    *
    */
    public markDocumentProcessed(documentId: string): Observable<Documents[]> {
    let url = this.config.url;
    if (!url.endsWith("/")) {
    url += "/";
    }

    const requestBody = {
    DocumentId: documentId,
    };

    return this.http
    .post<Documents[]>(`${url}api/v1/document/processed`, requestBody, {
    headers: {
    authorization: this.config.authorization,
    },
    })
    .pipe(
    map((data) => {
    return data;
    }),
    catchError((error) => {
    throw error;
    })
    );
    }

    /**
    * getDocumentImage: calls the api service to get the document image.
    *
    */
    public getDocumentImage(documentId: string): Observable<Blob> {
    let url = this.config.url;
    if (!url.endsWith("/")) {
    url += "/";
    }

    return this.http.get(`${url}api/v1/document/${documentId}/image/Photo`, {
    responseType: "blob",
    headers: {
    authorization: this.config.authorization,
    },
    });
    }


    I get no errors in the console, I get a 200 with the endpoint but my test client doesn't recieve the events automatically and I have to refresh to see the changes.

    Continue reading...

Compartilhe esta Página