How to intercept SSE Request in React?

247 views Asked by At

How to intercept SSE Request in React?

I am trying to add http authorization token to SSE (Server side events) in my react project for event source polyfill.

I am able to add it but the problem is when the SSE retries the connection after sometime when my authorization token expires.

It will be nice If I can have a way to intercept this request so that I can add the updated token..

If not I have to do a recursive call on the onerror callback which I dont feel comfortable..

import { EventSourcePolyfill } from 'event-source-polyfill';
const es = await new EventSourcePolyfill(url, {
            headers: {
                'Authorization': `Bearer ${token}`
            }
});
es.onmessage = onMessageCallbck;
es.onerror = (e: any) => {
    // Want to update the token like this but it does not work as headers are not exposed.
es.headers = {
    'Authorization': `Bearer ${newToken}`
}
0

There are 0 answers