Reputation: 123
I am working on one API/SSE where I get data in streams from backend. To recieve that on front end I am using eventSource. Its code is as below -
let source = new EventSource("url", {headers, responseType: 'text'});
source.addEventListener('message', message => {
let n: Notification;
n = JSON.parse(message.data);
console.log(message.data);
});
But what error I am facing is -
Argument of type '{ headers: HttpHeaders; responseType: string; }' is not assignable to parameter of type 'EventSourceInit'.
Object literal may only specify known properties, and 'headers' does not exist in type 'EventSourceInit'.
How can I add headers and responseType in eventSource?
Any help is appreciated.
Thanks a lot!
Upvotes: 4
Views: 7121
Reputation: 6235
You can't add it. EventSource()
itself does not accept such params (like the error message said too). What is the end-result you are looking for?
If you are trying to get the credentials, you could try this:
let source = new EventSource("url", { withCredentials: true } );
All possible params and methods of the API: https://developer.mozilla.org/en-US/docs/Web/API/EventSource
Upvotes: 2