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
eventsource
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.
sse.js
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.
reconnecting-eventsource
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.
@sanity/eventsource
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'
License
MIT © Sanity.io