Socket
Socket
Sign inDemoInstall

eventsource

Package Overview
Dependencies
0
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    eventsource

W3C compliant EventSource client for Node.js


Version published
Maintainers
1
Install size
46.1 kB
Created

Package description

What is eventsource?

The eventsource npm package is a Node.js implementation of the EventSource client, which is used for receiving server-sent events (SSE). It allows clients to subscribe to a stream of updates from a server over an HTTP connection. The package is designed to be compliant with the W3C EventSource specification and is typically used for real-time data feeds, such as live notifications, stock prices updates, or any other continuous data stream.

What are eventsource's main functionalities?

Connecting to an SSE server

This code demonstrates how to connect to an SSE server and listen for messages. The 'onmessage' handler is called whenever a new message is received, and the 'onerror' handler is called if there's an error with the connection.

const EventSource = require('eventsource');
const es = new EventSource('http://example.com/events');
es.onmessage = function(event) {
  console.log(event.data);
};
es.onerror = function(err) {
  console.error('EventSource failed:', err);
};

Listening for specific event types

This code snippet shows how to listen for specific event types using the 'addEventListener' method. In this example, the client listens for 'userupdate' events and processes the received data.

const EventSource = require('eventsource');
const es = new EventSource('http://example.com/events');
es.addEventListener('userupdate', function(event) {
  const userData = JSON.parse(event.data);
  console.log('User update:', userData);
});

Reconnecting after connection loss

This example illustrates the automatic reconnection feature of the EventSource API. The 'onopen' handler is called when the connection is successfully established, and the 'onerror' handler indicates that the connection has been lost and a reconnection attempt will be made.

const EventSource = require('eventsource');
const es = new EventSource('http://example.com/events');
es.onopen = function() {
  console.log('Connection to server opened.');
};
es.onerror = function() {
  console.log('Connection lost, the browser will automatically attempt to reconnect.');
};

Other packages similar to eventsource

Changelog

Source

0.1.3

  • Bugfix: Made message properties enumerable. (#37 Golo Roden)

Readme

Source

EventSource Build Status Dependencies Bitdeli Badge

NPM NPM

This library implements the EventSource client for Node.js. The API aims to be W3C compatible.

Install

npm install eventsource

Usage

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.

Example

See https://github.com/einaros/sse-example

Extensions to the W3C API

Setting HTTP request headers

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);

Allow unauthorized HTTPS requests

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.

Keywords

FAQs

Last updated on 17 Sep 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc