I’m experiencing a delay in receiving SSE events when using IIS with Integrated Pool mode. The delay seems to be specific to SSE events, as other types of content are not affected. I’ve tried disabling dynamic compression for the text/event-stream MIME type in web.config, but that didn’t make any difference.
I’m using Node.js on the backend to generate SSE events and Angular on the frontend to receive them.
Has anyone else encountered this problem? Any suggestions on how to fix it?
Here is my Node.js code:
app.get(Config.rootAPIPath + '/progress', (req, res) => {
// Set headers for Server-Sent Events
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
// Listen to progress events from business layer
const progressListener = (data) => {
res.write(`data: ${JSON.stringify(data)}nn`);
if (data.processed == 100) {
res.write(`event: completendata: {"processed": 100}nn`);
res.end();
}
};
Stream.on('progress', progressListener);
});
Here is my angular component code:
listenToServer() {
this.eventSubscription = this.sseService.getServerSentEvents(`${Config.serverSideEventURL}progress`)
.subscribe({
next: (event: any) => {
this.zone.run(() => {
console.log('Event received:', event);
this.progress = JSON.parse(event.data);
let counter = this.progress.processed;
if (isNullOrUndefined(counter))
this.counter = 0;
else
this.counter = +counter;
this.appRef.tick();
});
},
complete: () => {
console.log('SSE connection completed');
this.isImporting = false;
},
error: (error) => {
console.error('SSE error:', error);
this.isImporting = false;
}
});
}
Here is my Service code:
export class ServerSideEventService {
constructor(private ngZone: NgZone) { }
getServerSentEvents(url: string): Observable<any> {
return new Observable(observer => {
const eventSource = new EventSource(url);
eventSource.onmessage = (event) => {
this.ngZone.run(() => {
observer.next(event);
});
};
eventSource.addEventListener('complete', (event) => {
this.ngZone.run(() => {
observer.complete();
});
});
eventSource.onerror = (error) => {
this.ngZone.run(() => {
observer.error(error);
});
};
return () => eventSource.close();
});
}
}
I’ve verified that the headers are being set correctly on both sides.
I’ve found some information online that suggests that IIS may compress SSE events in Classic Mode but not in Integrated Mode. My application pool is currently running in Integrated Mode, so I don’t think this is the issue.
partyDj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.