New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-eventsource

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-eventsource

React hook for Server-Sent Events with custom headers support, built on @microsoft/fetch-event-source

latest
Source
npmnpm
Version
2.1.0
Version published
Maintainers
2
Created
Source

React EventSource

React hook for Server-Sent Events with custom headers support

npm version Downloads/week License: MIT

A simple React hook for Server-Sent Events with the key feature that native EventSource lacks: custom headers support.

Perfect for authenticated Server-Sent Events, API keys, and any scenario where you need to send headers with your SSE connection.

Installation

npm install react-eventsource

Quick Start

import React from 'react'
import { useSSE } from 'react-eventsource'

function ServerUpdates() {
  const { readyState, close, reconnect } = useSSE({
    url: '/api/events',
    headers: {
      'Authorization': 'Bearer your-token',
      'X-API-Key': 'your-api-key'
    },
    onMessage: (message) => {
      console.log('Received:', message.data)
    },
    onError: (error) => {
      console.error('SSE Error:', error)
    }
  })

  const status = ['CONNECTING', 'OPEN', 'CLOSED'][readyState]

  return (
    <div>
      <p>Connection: {status}</p>
      <button onClick={close}>Close</button>
      <button onClick={reconnect}>Reconnect</button>
    </div>
  )
}

API Reference

useSSE(options)

Options

PropertyTypeRequiredDescription
urlstringYesServer-Sent Events endpoint URL
headersRecord<string, string>NoCustom headers (auth, API keys, etc.)
methodstringNoHTTP method (defaults to 'GET')
bodystring | FormDataNoRequest body (rarely needed for SSE)
onMessage(message: EventSourceMessage) => voidNoMessage event handler
onOpen(response: Response) => voidNoConnection open handler
onError(error: unknown) => voidNoError handler
fetchtypeof window.fetchNoCustom fetch implementation
openWhenHiddenbooleanNoKeep connection open when page is hidden (defaults to true)

Return Values

PropertyTypeDescription
readyStatenumberConnection state: 0=CONNECTING, 1=OPEN, 2=CLOSED
close() => voidManually close the connection
reconnect() => voidClose current connection and immediately reconnect

TypeScript Support

The hook is fully typed and exports the following types:

import { useSSE, type UseSSEOptions, type UseSSEResult } from 'react-eventsource'

// Type your own variables
const config: UseSSEOptions = {
  url: '/api/events',
  headers: { 'Authorization': 'Bearer token' }
}

// Extend the interface if needed
interface MySSEOptions extends UseSSEOptions {
  retryCount?: number
}

Examples

Authenticated Events

useSSE({
  url: '/api/user-notifications',
  headers: {
    'Authorization': `Bearer ${userToken}`
  },
  onMessage: (msg) => {
    const notification = JSON.parse(msg.data)
    showNotification(notification)
  }
})

POST with Body (Advanced)

useSSE({
  url: '/api/stream',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-User-ID': userId
  },
  body: JSON.stringify({ 
    channels: ['updates', 'alerts'] 
  }),
  onMessage: handleStreamMessage
})

Connection Management

function ChatStream() {
  const { readyState, close, reconnect } = useSSE({
    url: '/api/chat-stream',
    headers: { 'Authorization': `Bearer ${token}` },
    onMessage: (msg) => addMessage(JSON.parse(msg.data)),
    onError: (err) => console.error('Chat stream error:', err)
  })

  const isConnected = readyState === 1

  return (
    <div>
      <div className={`status ${isConnected ? 'connected' : 'disconnected'}`}>
        {isConnected ? '🟢 Connected' : '🔴 Disconnected'}
      </div>
      
      <button onClick={reconnect} disabled={readyState === 0}>
        Reconnect
      </button>
      
      <button onClick={close}>
        Disconnect
      </button>
    </div>
  )
}

Page Visibility Behavior

By default, this hook keeps SSE connections open even when the browser tab/window is hidden (minimized, switched away, etc.). This matches the behavior of the native EventSource API.

If you want to close the connection when the page is hidden and automatically reconnect when visible again (to reduce server load), set openWhenHidden to false:

useSSE({
  url: '/api/events',
  openWhenHidden: false,  // Close connection when page is hidden
  onMessage: (msg) => console.log(msg.data)
})

When to use openWhenHidden: false:

  • You want to reduce server load by closing idle connections
  • Your app doesn't need real-time updates when the user isn't viewing the page
  • You're implementing a background sync that should pause when hidden

When to keep the default (true):

  • You need continuous updates regardless of page visibility
  • You're building a real-time monitoring dashboard
  • Missing events while hidden would cause data inconsistency

Why This Hook?

  • Native EventSource limitation: Cannot send custom headers
  • This hook solves that: Built on @microsoft/fetch-event-source which supports headers
  • React-friendly: Manages connection lifecycle with useEffect
  • Simple API: Familiar EventSource-like interface
  • TypeScript: Full type safety out of the box

License

MIT License. See LICENSE for details.

Keywords

React

FAQs

Package last updated on 31 Oct 2025

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