I’m trying to receive Server-sent events (SSE) in the browser. My application uses axios (v1.6.8) to make API calls. This code (courtesy of this answer) works in node:
const getStreamAxios = () => {
axios.get('https://my-domain.com/api/getStream', {
responseType: 'stream',
headers: {
'Accept': 'text/event-stream',
}
})
.then(response => {
console.log('axios got a response');
const stream = response.data;
stream.on('data', data => {
console.log(data.toString('utf8'));
});
})
.catch(e => {
console.error('got an error', e);
});
}
But in the browser, the Promise
is never fulfilled (i.e.: I don’t get the console message “axios got a response”). Instead I get this warning in the console:
The provided value ‘stream’ is not a valid enum value of type XMLHttpRequestResponseType.
The issue is not in the API endpoint; in addition to checking it with node, I have verified it also works with Apidog and EventSource
in the browser.