Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
@sanity/eventsource
Advanced tools
EventSource polyfill for browser and node.js
@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.
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);
};
The 'eventsource' package is another polyfill for the EventSource API. It provides similar functionality to @sanity/eventsource, allowing you to receive server-sent events in environments that do not natively support EventSource. Both packages offer similar APIs and can be used interchangeably.
The 'sse.js' package is a lightweight library for handling server-sent events. It provides a simple API for establishing connections and listening for events. Compared to @sanity/eventsource, sse.js is more minimalistic and may lack some advanced features but is easier to use for basic use cases.
The 'reconnecting-eventsource' package extends the EventSource API with automatic reconnection capabilities. This package is useful if you need a more robust solution that can handle intermittent network issues. It offers additional features compared to @sanity/eventsource, such as configurable reconnection strategies.
Meta-package to make browsers and Node use different EventSource polyfills
FAQs
EventSource polyfill for browser and node.js
The npm package @sanity/eventsource receives a total of 236,633 weekly downloads. As such, @sanity/eventsource popularity was classified as popular.
We found that @sanity/eventsource demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 48 open source maintainers collaborating on the project.
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.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.