npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

axios-sse

v1.0.1

Published

Lightweight Server-Sent Events (SSE) client for Axios with auto-reconnect and JSON parsing

Readme

axios-sse

npm version License: MIT

A lightweight Server-Sent Events (SSE) client built on Axios with auto-reconnect and JSON parsing capabilities.

Features

  • 🚀 Built on Axios - Leverage the power and flexibility of Axios
  • 🔄 Auto Reconnection - Automatic reconnection with configurable retry logic
  • 📦 JSON Auto-parsing - Automatically parse JSON data in SSE messages
  • 🎯 TypeScript Support - Full TypeScript support with type definitions
  • 🔐 Authentication - Support for custom headers and authentication
  • Lightweight - Minimal dependencies and small bundle size
  • 🛠️ Flexible Configuration - Customizable reconnection intervals and retry limits

Installation

npm install axios-sse
# or
pnpm install axios-sse
# or
yarn add axios-sse

CDN Usage

<!-- Include Axios first -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- Then include axios-sse -->
<script src="https://unpkg.com/axios-sse/dist/index.browser.js"></script>

<script>
  const sse = new AxiosSSE('https://api.example.com/events');
  sse.addEventListener('message', (event) => {
    console.log('Received:', event.data);
  });
</script>

Quick Start

import { AxiosSSE } from 'axios-sse';

// Basic usage
const sse = new AxiosSSE('https://api.example.com/events');

sse.addEventListener('message', (event) => {
  console.log('Received:', event.data);
});

// Clean up when done
sse.close();

API Reference

Constructor

The AxiosSSE class supports multiple constructor overloads for maximum flexibility:

// Basic usage with URL only
new AxiosSSE(url: string)

// With SSE configuration
new AxiosSSE(url: string, config: SSEConfig)

// With custom Axios instance
new AxiosSSE(url: string, axiosInstance: AxiosInstance)

// With custom Axios instance and SSE configuration
new AxiosSSE(url: string, axiosInstance: AxiosInstance, config: SSEConfig)

SSEConfig

interface SSEConfig {
  reconnectInterval?: number;  // Reconnection interval in ms (default: 3000)
  maxRetries?: number;         // Maximum retry attempts (default: 5)
  autoConnect?: boolean;       // Auto-connect on instantiation (default: true)
}

Methods

  • connect() - Manually start the SSE connection
  • close() - Close the SSE connection
  • readyState - Get current connection state (0: connecting, 1: open, 2: closed)

Events

  • message - Fired when a message is received
  • error - Fired when an error occurs

Usage Examples

Basic Usage

import { AxiosSSE } from 'axios-sse';

const sse = new AxiosSSE('https://api.example.com/events');

sse.addEventListener('message', (event) => {
  console.log('Message:', event.data);
});

sse.addEventListener('error', (event) => {
  console.error('Error:', event.detail);
});

With Authentication

import axios from 'axios';
import { AxiosSSE } from 'axios-sse';

// Create authenticated Axios instance
const authenticatedAxios = axios.create({
  headers: {
    'Authorization': 'Bearer your-token-here'
  }
});

const sse = new AxiosSSE('https://api.example.com/events', authenticatedAxios);

Custom Configuration

import { AxiosSSE } from 'axios-sse';

const sse = new AxiosSSE('https://api.example.com/events', {
  reconnectInterval: 5000,  // Retry every 5 seconds
  maxRetries: 10,           // Maximum 10 retry attempts
  autoConnect: false        // Don't connect automatically
});

// Manually start connection
sse.connect();

Manual Connection Control

import { AxiosSSE } from 'axios-sse';

// Create instance without auto-connecting
const sse = new AxiosSSE('https://api.example.com/events', {
  autoConnect: false
});

// Check connection state
console.log(sse.readyState); // 0 (not connected)

// Start connection when ready
sse.connect();
console.log(sse.readyState); // 1 (connected)

// Close connection
sse.close();
console.log(sse.readyState); // 2 (closed)

Advanced Usage with Custom Axios Configuration

import axios from 'axios';
import { AxiosSSE } from 'axios-sse';

const customAxios = axios.create({
  baseURL: 'https://api.example.com',
  headers: {
    'Authorization': 'Bearer token',
    'Custom-Header': 'value'
  },
  timeout: 30000
});

const sse = new AxiosSSE('/events', customAxios, {
  reconnectInterval: 2000,
  maxRetries: 3
});

SSE Message Format

The client automatically parses SSE messages according to the standard format:

id: message-id
event: custom-event-type
data: {"key": "value"}

The parsed message will have the following structure:

interface SSEMessage {
  id?: string;      // Message ID (if provided)
  event?: string;   // Event type (if provided)
  data: any;        // Parsed JSON data or raw string
}

Error Handling

import { AxiosSSE } from 'axios-sse';

const sse = new AxiosSSE('https://api.example.com/events');

sse.addEventListener('error', (event) => {
  console.error('SSE Error:', event.detail);
  
  // The client will automatically attempt to reconnect
  // based on the configured retry settings
});

TypeScript Support

This package includes full TypeScript support with type definitions:

import { AxiosSSE, SSEConfig, SSEMessage } from 'axios-sse';
import { AxiosInstance } from 'axios';

const config: SSEConfig = {
  reconnectInterval: 3000,
  maxRetries: 5,
  autoConnect: true
};

const sse = new AxiosSSE('https://api.example.com/events', config);

sse.addEventListener('message', (event: MessageEvent) => {
  const message: SSEMessage = event.data;
  console.log(message.data);
});

Browser Compatibility

This package works in all modern browsers that support:

  • EventTarget API
  • AbortController API
  • Axios requirements

License

MIT © Steven-Qiang

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Changelog

See CHANGELOG.md for details about changes in each version.