Socket
Socket
Sign inDemoInstall

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/


Version published
Weekly downloads
508K
decreased by-16.72%
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

FAQs

Package last updated on 22 Aug 2020

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc