Angular with Server sent event

Viewed 1593

How can i use server sent event with angular 6. I need to be getting data from backend periodically. I used it the normal way but it's not working

this.source=EventSource(url);

this.source.onmessage=(event) =>alert(event.data);

1 Answers

You should instantiate the this.source object with a new EventSource like this:

this.source = new EventSource(url);

You can also add an event listener for messages that come in from your backend like this:

this.source.addEventListener('message', message => { alert(message.data); });

Use the network tab within your browser's developer tools to ensure that the connection to the SSE is established and that messages are being received by the client.

Related