Skip to content
This repository was archived by the owner on Apr 15, 2023. It is now read-only.

Commit c34e0f2

Browse files
committed
Allow headers to be passed through to EventSource
Squashed commit of the following: commit af6df1cfef7b92085e3dff4096bde8ee42b0341d Author: Jordan Byron <[email protected]> Date: Tue Feb 20 21:13:06 2018 -0500 Fix README commit 69e229d21f6a527a23f994ed96fbf1e9fbca8cf1 Merge: 67682d6 fae9256 Author: Jordan Byron <[email protected]> Date: Tue Feb 20 21:08:21 2018 -0500 Merge branch 'master' of https://github.com/dflourusso/react-native-event-source into dflourusso-master commit fae9256 Author: Daniel Fernando Lourusso <[email protected]> Date: Thu Feb 15 08:25:26 2018 -0200 Update README.md commit b3ab975 Author: Daniel Fernando Lourusso <[email protected]> Date: Fri Jan 19 15:01:03 2018 -0200 add options to accept headers
1 parent 67682d6 commit c34e0f2

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

EventSource.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
var reTrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
88

9-
var EventSource = function (url) {
9+
var EventSource = function (url, options) {
1010
var eventsource = this,
1111
interval = 500, // polling interval
1212
lastEventId = null,
@@ -18,6 +18,7 @@ var EventSource = function (url) {
1818
}
1919

2020
this.URL = url;
21+
this.OPTIONS = options;
2122
this.readyState = this.CONNECTING;
2223
this._pollTimer = null;
2324
this._xhr = null;
@@ -35,6 +36,11 @@ var EventSource = function (url) {
3536
// NOTE: IE7 and upwards support
3637
var xhr = new XMLHttpRequest();
3738
xhr.open('GET', eventsource.URL, true);
39+
if (eventsource.OPTIONS && eventsource.OPTIONS.headers) {
40+
Object.keys(eventsource.OPTIONS.headers).forEach(key => {
41+
xhr.setRequestHeader(key, eventsource.OPTIONS.headers[key]);
42+
});
43+
}
3844
xhr.setRequestHeader('Accept', 'text/event-stream');
3945
xhr.setRequestHeader('Cache-Control', 'no-cache');
4046
// we must make use of this on the server side if we're working with Android - because they don't trigger

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ eventSource.addEventListener('message', (event) => {
3030
});
3131
```
3232

33+
You can also optionally pass a second argument when initializing `RNEventSource` to pass in header values:
34+
35+
```js
36+
const options = { headers: { Authorization: 'Baerer ...' } };
37+
const eventSource = new RNEventSource('https://my-sse.com/stream', options);
38+
```
39+
3340
Here is a full example that subscribes to a SSE stream and writes the results to `console.log`
3441

3542
```js

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import EventSource from './EventSource'
22

33
class RNEventSource {
4-
constructor(url) {
4+
constructor(url, options) {
55
this.url = url;
6-
this.eventSource = new EventSource(url);
6+
this.options = options;
7+
this.eventSource = new EventSource(url, options);
78
this.listeners = [];
89
}
910
addEventListener (type, listener) {

0 commit comments

Comments
 (0)