You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

event-source-polyfill

Package Overview
Dependencies
Maintainers
2
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-source-polyfill

A polyfill for http://www.w3.org/TR/eventsource/

1.0.31
latest
Source
npm
Version published
Weekly downloads
642K
-6.95%
Maintainers
2
Weekly downloads
 
Created

What is event-source-polyfill?

The event-source-polyfill package provides a polyfill for the EventSource API, which is used for receiving server-sent events (SSE). This is particularly useful for environments that do not natively support EventSource, such as older browsers or certain server-side JavaScript environments.

What are event-source-polyfill's main functionalities?

Basic EventSource Polyfill

This code demonstrates how to use the event-source-polyfill to create an EventSource instance that listens for server-sent events from a specified URL. It sets up handlers for incoming messages and errors.

const EventSource = require('event-source-polyfill');

const es = new EventSource('http://example.com/events');

es.onmessage = function(event) {
  console.log('New message:', event.data);
};

es.onerror = function(error) {
  console.error('Error:', error);
};

Custom Event Handlers

This code shows how to add custom event listeners for specific event types using the event-source-polyfill. In this example, a custom event named 'customEvent' is handled.

const EventSource = require('event-source-polyfill');

const es = new EventSource('http://example.com/events');

es.addEventListener('customEvent', function(event) {
  console.log('Custom event received:', event.data);
});

es.onerror = function(error) {
  console.error('Error:', error);
};

Reconnection Logic

This code demonstrates how to handle reconnection logic using the event-source-polyfill. It listens for errors and attempts to reconnect if the connection is closed.

const EventSource = require('event-source-polyfill');

const es = new EventSource('http://example.com/events', { withCredentials: true });

es.onopen = function() {
  console.log('Connection opened');
};

es.onmessage = function(event) {
  console.log('New message:', event.data);
};

es.onerror = function(error) {
  console.error('Error:', error);
  if (es.readyState === EventSource.CLOSED) {
    console.log('Reconnecting...');
    es = new EventSource('http://example.com/events', { withCredentials: true });
  }
};

Other packages similar to event-source-polyfill

Keywords

sse

FAQs

Package last updated on 11 Aug 2022

Did you know?

Socket

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