How camflan/use-event-source works
Main Components
a) Client-Side Components:
• React Application
• useEventSource Hook
• EventSource Interface
• State Management (external - user provided)
b) Server-Side Components:
• SSE Server Endpoint (external)
┌─ Client Application Layer ─────────────────────┐
• ┌─ React Component ─────────────────────┐ │
• ┌─ useEventSource Hook ──────────┐ │ │
• Configuration │ │ │
• Connection Management │ │ │
• Error Handling │ │ │
• ┬─────────────┘ │ │
• ┼─────────────────┘ │
• ┌─ EventSource ─────┼───────────────────┐ │
• Native Browser API│ │ │
• ┬─┘ │ │
• ┼─────────────────────┘ │
┌───────────────────▼──────────────────────┐
• SSE Server Endpoint (External) │
• ┘
Important Flows to Highlight
a) Connection Flow:
• Component initialization → Hook initialization → EventSource connection
• Manual connection through openStream()
• Disconnection through closeStream()
b) Data Flow:
• SSE Server → EventSource → Hook → onMessage callback → User's state management
c) Error Flow:
• Error occurrence → Hook error state → Component error handling
States
• Connection States:
• "connected"
• "connecting"
• "disconnected"
Implementation Details
• Hook return values:
• {
• error: Error
• connectionStatus: string
• openStream: () => void
• closeStream: () => void
• }
Notes
• Keep the focus on the hook's interaction with EventSource
• Clear boundaries between library code and user code
• Event-driven nature of the system
• Configuration options
• Error propagation paths
Remember to indicate that the SSE server is external to the system, as this library only handles the client-side implementation of SSE connections.