Skip to content

Commit

Permalink
Update README and add debug functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetazzoni committed Sep 1, 2023
1 parent 6dd5a85 commit 26f25c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
72 changes: 37 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EventSource = SSE;
var source = new SSE(url, options);
```

## Basic usage
## Usage

The most simple way to use `SSE` is to create the `SSE` object, attach
one or more listeners, and activate the stream:
Expand All @@ -48,6 +48,42 @@ source.addEventListener('message', (e) => { ... });
source.stream();
```

### Passing custom headers

```js
var source = new SSE(url, {headers: {'Authorization': 'Bearer 0xdeadbeef'}});
```

### Making a POST request and overriding the HTTP method

To make a HTTP POST request, simply specify a `payload` in the options:

```js
var source = new SSE(url, {headers: {'Content-Type': 'text/plain'},
payload: 'Hello, world!'});
```

Alternatively, you can also manually override the HTTP method used to
perform the request, regardless of the presence of a `payload` option, by
specifying the `method` option:

```js
var source = new SSE(url, {headers: {'Content-Type': 'text/plain'},
payload: 'Hello, world!',
method: 'GET'});
```

### Options reference

| Name | Description |
| ----------------- | ----------- |
| `headers` | A map of additional headers to use on the HTTP request |
| `method` | Override HTTP method (defaults to `GET`, unless a payload is given, in which case it defaults to `POST`) |
| `payload` | An optional request payload to sent with the request |
| `withCredentials` | If set to `true`, CORS requests will be set to include credentials |
| `start` | Automatically execute the request and start streaming (defaults to `true`) |
| `debug` | Log debug messages to the console about received chunks and dispatched events (defaults to `false`) |

## Events

`SSE` implements the `EventTarget` interface (just like `EventSource`)
Expand Down Expand Up @@ -100,30 +136,6 @@ source.onstatus = function(e) { ... };
You can mix both `on<event>` and `addEventListener()`. The `on<event>`
handler is always called first if it is defined.

## Passing custom headers

```js
var source = new SSE(url, {headers: {'Authorization': 'Bearer 0xdeadbeef'}});
```

## Making a POST request and overriding the HTTP method

To make a HTTP POST request, simply specify a `payload` in the options:

```js
var source = new SSE(url, {headers: {'Content-Type': 'text/plain'},
payload: 'Hello, world!'});
```

Alternatively, you can also manually override the HTTP method used to
perform the request, regardless of the presence of a `payload` option, by
specifying the `method` option:

```js
var source = new SSE(url, {headers: {'Content-Type': 'text/plain'},
payload: 'Hello, world!',
method: 'GET'});
```

## Expected response from server

Expand All @@ -143,16 +155,6 @@ request that the outgoing HTTP request be made with a CORS credentials
mode of `include`, as per the [HTML Living
Standard](https://fetch.spec.whatwg.org/#concept-request-credentials-mode).

## Options reference

| Name | Description |
| ----------------- | ----------- |
| `headers` | A map of additional headers to use on the HTTP request |
| `method` | Override HTTP method (defaults to `GET`, unless a payload is given, in which case it defaults to `POST`) |
| `payload` | An optional request payload to sent with the request |
| `withCredentials` | If set to `true`, CORS requests will be set to include credentials |
| `start` | Automatically execute the request and start streaming (defaults to `true`) |

## TODOs and caveats

- Internet Explorer 11 does not support arbitrary values in
Expand Down
9 changes: 9 additions & 0 deletions lib/sse.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var SSE = function (url, options) {
this.payload = options.payload !== undefined ? options.payload : '';
this.method = options.method || (this.payload && 'POST' || 'GET');
this.withCredentials = !!options.withCredentials;
this.debug = !!options.debug;

this.FIELD_SEPARATOR = ':';
this.listeners = {};
Expand Down Expand Up @@ -62,6 +63,10 @@ var SSE = function (url, options) {
return true;
}

if (this.debug) {
console.debug(e);
}

e.source = this;

var onHandler = 'on' + e.type;
Expand Down Expand Up @@ -150,6 +155,10 @@ var SSE = function (url, options) {
return null;
}

if (this.debug) {
console.debug(chunk);
}

var e = {'id': null, 'retry': null, 'data': null, 'event': 'message'};
chunk.split(/\n|\r\n|\r/).forEach(function(line) {
line = line.trimRight();
Expand Down

0 comments on commit 26f25c2

Please sign in to comment.