EventSource and basic http authentication

Viewed 11139

Does anyone know if it is possible to send basic http authentication credentials with EventSource?

5 Answers

You can use event-source-polyfill to add headers like this

import { EventSourcePolyfill } from 'event-source-polyfill';

new EventSourcePolyfill(`/api/liveUpdate`, {
  headers: {
    Authorization: `Bearer 12345`,
    'x-csrf-token': `xxx-xxx-xxx`,
  },
});

Nowadays there is a NPM package to change the HTTP Header

https://www.npmjs.com/package/eventsource

This library is a pure JavaScript implementation of the EventSource client. The API aims to be W3C compatible.

You can use it with Node.js or as a browser polyfill for browsers that don't have native EventSource support.

Related