Socket
Socket
Sign inDemoInstall

@sanity/eventsource

Package Overview
Dependencies
2
Maintainers
35
Versions
565
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @sanity/eventsource

EventSource polyfill for browser and node.js


Version published
Weekly downloads
270K
increased by1.41%
Maintainers
35
Install size
368 kB
Created
Weekly downloads
 

Package description

What is @sanity/eventsource?

@sanity/eventsource is a JavaScript library that provides a polyfill for the EventSource API, which is used for receiving server-sent events (SSE). This package allows you to establish a persistent connection to a server and receive updates in real-time, making it useful for applications that require live data updates, such as chat applications, live feeds, or notifications.

What are @sanity/eventsource's main functionalities?

Establishing a Connection

This feature allows you to establish a connection to a server that sends events. The code sample demonstrates how to create a new EventSource instance and listen for incoming messages.

const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.onmessage = function(event) {
  console.log('New message:', event.data);
};

Handling Different Event Types

This feature allows you to listen for specific types of events sent by the server. The code sample shows how to add an event listener for a custom event type.

const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.addEventListener('customEvent', function(event) {
  console.log('Custom event received:', event.data);
});

Handling Connection Errors

This feature allows you to handle errors that occur during the connection. The code sample demonstrates how to set up an error handler for the EventSource instance.

const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.onerror = function(event) {
  console.error('Error occurred:', event);
};

Other packages similar to @sanity/eventsource

Changelog

Source

4.1.1 (2023-03-22)

Bug Fixes

  • deps: update dependency event-source-polyfill to v1.0.31 (f385961)
  • rollback TS (6803c69)

Readme

Source

@sanity/eventsource

npm stat npm version gzip size size

Meta-package to make browsers and Node use different EventSource polyfills

npm install @sanity/eventsource

Usage

import polyfilledEventSource from '@sanity/eventsource'

The polyfill adds support for auth headers, which isn't part of the EventSource spec.

In NodeJS environments it's the implementation from eventsource package. While browsers use event-source-polyfill.

Forcing a specific implementation

To get the browser implementation, no matter how your bundler or runtime understands package.json exports, you can use the following:

import polyfilledEventSource from '@sanity/eventsource/browser'

To force the Node implementation, use the following:

import polyfilledEventSource from '@sanity/eventsource/node'

Release new version

Run "CI & Release" workflow. Make sure to select the main branch and check "Release new version".

Semantic release will only release on configured branches, so it is safe to run release on any branch.

License

MIT © Sanity.io

Keywords

FAQs

Last updated on 22 Mar 2023

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