Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@microsoft/fetch-event-source

Package Overview
Dependencies
Maintainers
5
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@microsoft/fetch-event-source

A better API for making Event Source requests, with all the features of fetch()

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
458K
decreased by-4.81%
Maintainers
5
Weekly downloads
 
Created

What is @microsoft/fetch-event-source?

@microsoft/fetch-event-source is a JavaScript library that provides a simple way to handle Server-Sent Events (SSE) using the Fetch API. It allows you to receive real-time updates from a server over an HTTP connection.

What are @microsoft/fetch-event-source's main functionalities?

Basic Event Source

This feature allows you to connect to an SSE endpoint and handle incoming messages. The `onmessage` callback is triggered whenever a new message is received, and the `onerror` callback handles any errors.

const { fetchEventSource } = require('@microsoft/fetch-event-source');

fetchEventSource('https://example.com/sse', {
  onmessage(event) {
    console.log('New message:', event.data);
  },
  onerror(err) {
    console.error('Error:', err);
  }
});

Custom Headers

This feature allows you to include custom headers in your SSE request. This is useful for scenarios where you need to pass authentication tokens or other custom headers.

const { fetchEventSource } = require('@microsoft/fetch-event-source');

fetchEventSource('https://example.com/sse', {
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN'
  },
  onmessage(event) {
    console.log('New message:', event.data);
  },
  onerror(err) {
    console.error('Error:', err);
  }
});

Reconnection Logic

This feature demonstrates how to implement reconnection logic when the SSE connection is closed. The `onclose` callback is used to attempt reconnection after a specified delay.

const { fetchEventSource } = require('@microsoft/fetch-event-source');

fetchEventSource('https://example.com/sse', {
  onmessage(event) {
    console.log('New message:', event.data);
  },
  onerror(err) {
    console.error('Error:', err);
  },
  onclose() {
    console.log('Connection closed, attempting to reconnect...');
    setTimeout(() => {
      fetchEventSource('https://example.com/sse', options);
    }, 5000);
  }
});

Other packages similar to @microsoft/fetch-event-source

FAQs

Package last updated on 25 Dec 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