I need to set an Authorization header to an HTML5 EventSource. As Server Sent Events seems to be disused since Websockets appeared, I cannot find any useful documentation. The a
The window.EventSource
doesn't seem to support passing additional headers yet. Good news is there are some other implementations of EventSource
that support additional headers. Some of them are as follows:
const eventSource = new EventSource(resoureUrl, {
headers: {
'Authorization': 'Bearer ' + authorizationToken;
}
});
es.onmessage = result => {
const data = JSON.parse(result.data);
console.log('Data: ', data);
};
es.onerror = err => {
console.log('EventSource error: ', err);
};