Skip to content

Commit

Permalink
renamed sse endpoint to events. Using the technology name in the API …
Browse files Browse the repository at this point in the history
…isnt a good idea.
  • Loading branch information
AnalogJ committed Sep 10, 2023
1 parent 6a3afe1 commit 74fc682
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/pkg/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (ae *AppEngine) Setup() (*gin.RouterGroup, *gin.Engine) {
secure.POST("/query", handler.QueryResourceFhir)

//server-side-events handler
secure.GET("/sse/stream", middleware.SSEHeaderMiddleware(), middleware.SSEEventBusServerMiddleware(), handler.SSEStream)
secure.GET("/events/stream", middleware.SSEHeaderMiddleware(), middleware.SSEEventBusServerMiddleware(), handler.SSEStream)
}

if ae.Config.GetBool("web.allow_unsafe_endpoints") {
Expand Down
30 changes: 29 additions & 1 deletion frontend/src/app/components/toast/toast.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import {Component, OnInit, TemplateRef} from '@angular/core';
import {ToastService} from '../../services/toast.service';
import {AuthService} from '../../services/auth.service';
import {FastenApiService} from '../../services/fasten-api.service';
import {Subscription} from 'rxjs';
import {NavigationEnd, Router} from '@angular/router';

@Component({
selector: 'app-toast',
templateUrl: './toast.component.html',
styleUrls: ['./toast.component.scss']
})
export class ToastComponent implements OnInit {
routerSubscription: Subscription | undefined;

constructor(public toastService: ToastService) {}
constructor(
public router: Router,
public toastService: ToastService,
public authService: AuthService,
public fastenApiService: FastenApiService,
) {}

ngOnInit(): void {

}
ngAfterViewInit() {
//TODO: this is a bit kludgey.
// Ideally we want consistently listen to events, but only when the user is authenticated.
this.routerSubscription = this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
if(!event.url.startsWith("/auth") && this.authService.IsAuthenticated()){
console.log("user is authenticated, listening for notifications")
//user is authenticated, lets start listening for notifications
this.routerSubscription?.unsubscribe()
this.fastenApiService.listenEventBus().subscribe((event)=>{
console.log("eventbus event:", event)
//TODO: start toasts.
})
}
}
});
}
}
24 changes: 24 additions & 0 deletions frontend/src/app/services/fasten-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as fhirpath from 'fhirpath';
import _ from 'lodash';
import {DashboardConfig} from '../models/widget/dashboard-config';
import {DashboardWidgetQuery} from '../models/widget/dashboard-widget-query';
import { fetchEventSource } from '@microsoft/fetch-event-source';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -54,6 +55,29 @@ export class FastenApiService {
SECURE ENDPOINTS
*/

listenEventBus(): Observable<any> {
let eventStreamUrl = `${GetEndpointAbsolutePath(globalThis.location, environment.fasten_api_endpoint_base)}/secure/events/stream`

return new Observable(observer => {
fetchEventSource(eventStreamUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.authService.GetAuthToken()}`
},
onmessage(ev) {
observer.next(ev.data);
},
onerror(event) {
observer.error(event)
},
}).then(
() => observer.complete(),
error => observer.error(error)
)
});
}

getDashboards(): Observable<DashboardConfig[]> {
return this._httpClient.get<any>(`${GetEndpointAbsolutePath(globalThis.location, environment.fasten_api_endpoint_base)}/secure/dashboards`, )
.pipe(
Expand Down

0 comments on commit 74fc682

Please # to comment.