vlad 6f123ff03e 18.07 | il y a 6 ans | |
---|---|---|
.. | ||
lib | il y a 6 ans | |
test | il y a 6 ans | |
.npmignore | il y a 6 ans | |
.travis.yml | il y a 6 ans | |
CONTRIBUTING.md | il y a 6 ans | |
History.md | il y a 6 ans | |
LICENSE | il y a 6 ans | |
README.md | il y a 6 ans | |
example.js | il y a 6 ans | |
package.json | il y a 6 ans |
This library implements the EventSource client for Node.js. The API aims to be W3C compatible.
npm install eventsource
var EventSource = require('eventsource');
var es = new EventSource('http://demo-eventsource.rhcloud.com/');
es.onmessage = function(e) {
console.log(e.data);
};
es.onerror = function() {
console.log('ERROR!');
};
See the spec for API docs.
See https://github.com/einaros/sse-example
You can define custom HTTP headers for the initial HTTP request. This can be useful for e.g. sending cookies
or to specify an initial Last-Event-ID
value.
HTTP headers are defined by assigning a headers
attribute to the optional eventSourceInitDict
argument:
var eventSourceInitDict = {headers: {'Cookie': 'test=test'}};
var es = new EventSource(url, eventSourceInitDict);
By default, https requests that cannot be authorized will cause connection to fail and an exception to be emitted. You can override this behaviour:
var eventSourceInitDict = {rejectUnauthorized: false};
var es = new EventSource(url, eventSourceInitDict);
Note that for Node.js < v0.10.x this option has no effect - unauthorized HTTPS requests are always allowed.
Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the status
property in the error event.
es.onerror = function (err) {
if (err) {
if (err.status === 401 || err.status === 403) {
console.log('not authorized');
}
}
};